mutterings of a cynic

Thursday, June 14, 2007

interface on the fly

On rahel's suggestion I thought I should post something really useful that you can do with generics. In java you can map most things by just adding another interface but sometimes you really don't want to define a crappy interface like:


interface DisposableComponent {
  void dispose();
  Component getComponent();
}


So here's something that not everyone knows about generics:


interface Flappable {
  void flap();
}

interface Flippable {
  void flip();
}

class FlipFlapper {
  <T extends Flappable & Flippable> void flipAndFlap(T ff) {
    ff.flip();
    ff.flap();
  }
}

Labels: ,


1 Comments:

Post a Comment

<< Home