我有一个通用接口
public interface Consumer<E> { public void consume(E e); }
我有一个使用两种类型对象的类,所以我想做类似的事情:
public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple> { public void consume(Tomato t) { ..... } public void consume(Apple a) { ...... } }
显然我不能那样做。
我当然可以自己实现调度,例如
public class TwoTypesConsumer implements Consumer<Object> { public void consume(Object o) { if (o instanceof Tomato) { ..... } else if (o instanceof Apple) { ..... } else { throw new IllegalArgumentException(...) } } }
但我正在寻找泛型提供的编译时类型检查和调度解决方案。
我能想到的最好的解决方案是定义单独的接口,例如
public interface AppleConsumer { public void consume(Apple a); }
从功能上讲,我认为这个解决方案还可以。它只是冗长而丑陋。
有任何想法吗?
考虑封装:
public class TwoTypesConsumer { private TomatoConsumer tomatoConsumer = new TomatoConsumer(); private AppleConsumer appleConsumer = new AppleConsumer(); public void consume(Tomato t) { tomatoConsumer.consume(t); } public void consume(Apple a) { appleConsumer.consume(a); } public static class TomatoConsumer implements Consumer<Tomato> { public void consume(Tomato t) { ..... } } public static class AppleConsumer implements Consumer<Apple> { public void consume(Apple a) { ..... } } }
如果创建这些静态内部类让您感到困扰,您可以使用匿名类:
public class TwoTypesConsumer { private Consumer<Tomato> tomatoConsumer = new Consumer<Tomato>() { public void consume(Tomato t) { } }; private Consumer<Apple> appleConsumer = new Consumer<Apple>() { public void consume(Apple a) { } }; public void consume(Tomato t) { tomatoConsumer.consume(t); } public void consume(Apple a) { appleConsumer.consume(a); } }