如何避免为泛型类型创建特定实现

时间:2013-11-09 20:53:12

标签: java generics

这是我尝试的示例程序。

我正在处理物品 - 单人或多人

public interface IItem<T> { 
    T getContents();    
}

/** An item that holds a Map and uses it for something  */
public class SingleItem implements IItem<Map<String,String>> {
    . . . . .
}

/** An item that holds a List of maps and uses it for something  */
public class MultiItem implements IItem<List<Map<String,String>>> {
    . . . . .
}

可以生成SingleItems或MultiItems的生产者

public interface IProducer<T> { 
    T produce();
}

public class SingleItemProducer implements IProducer<SingleItem> {

    @Override
    public SingleItem produce() {
        return new SingleItem(...);
    }
}

public class MultiItemProducer implements IProducer<MultiItem> {
    . . . . . 
    . . . . .
}

可以使用SingleItems或MultiItems的消费者

public interface IConsumer<T> {
    void consume(T item);
}

public class SingleItemConsumer implements IConsumer<SingleItem> {

    @Override
    public void consume(SingleItem item) {  
        System.out.println(item.getContents());
    }
}

public class MultiItemConsumer implements IConsumer<MultiItem> {
    . . . . .
    . . . . .     
}

将使用生产者和处理器的处理器消费者完成工作。

public interface IProcessor<T> {
    void process();
    void process(T item);
}

/** A generic processor that should be able to work with any type of consumer or producer */
public class GenericItemProcessor implements IProcessor<IItem<? extends Object>> {

    IProducer<IItem<? extends Object>> producer;
    IConsumer<IItem<? extends Object>> consumer;

    public GenericItemProcessor (IProducer<IItem<? extends Object>> p, IConsumer<IItem<? extends Object>> c) {
        this.producer = p;
        this.consumer = c;
    }

    @Override
    public void process() {
        IItem<? extends Object> item = null;
        // As long the producer produces items, keep processing them
        while ((item = producer.produce()) != null) {
            this.process(item);
        }
    }

    @Override
    public void process(IItem<? extends Object> item) {
        consumer.consume(item);
    }
}

而且,在主程序中,当我尝试这个时,

IProducer<SingleItem> producer = new SingleItemProducer();
IConsumer<SingleItem> consumer= new SingleItemConsumer();
IProcessor<IItem<? extends Object>> processor = new GenericItemProcessor(producer, consumer);

它说我不能做最后的任务。

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor GenericItemProcessor(IProducer<SingleItem>, IConsumer<SingleItem>) is undefined

当然,这样可行,

IProcessor<SingleItem> processor = new GenericItemProcessor(producer, consumer);

如果我在IItem<? extends Object>内用SingleItem替换GenericItemProcessor的所有出现。

但问题在于我必须创建一个处理器来处理每个项目类型,看起来它会破坏使用Generic'Typed'处理器的目的。

可能在这里遗漏了一些明显的东西,但是,这怎么能正确完成呢?非常感谢一些建议。

1 个答案:

答案 0 :(得分:1)

我认为您在GenericItemProcessor中对泛型的使用过于复杂。处理器中的代码不会使用处理器生成IItem个对象的事实,因此不必将泛型类型绑定到IItem<? extends Object>

/** A generic processor that should be able to work with any type of consumer or producer */
public class GenericProcessor<T> implements IProcessor<T> {

    IProducer<? extends T> producer;
    IConsumer<? super T> consumer;

    public GenericProcessor(IProducer<? extends T> p, IConsumer<? super T> c) {
        this.producer = p;
        this.consumer = c;
    }

    @Override
    public void process() {
        T item = null;
        // As long the producer produces items, keep processing them
        while ((item = producer.produce()) != null) {
            this.process(item);
        }
    }

    @Override
    public void process(T item) {
        consumer.consume(item);
    }
}

所以这段代码现在没问题了:

IProducer<SingleItem> producer = new SingleItemProducer();
IConsumer<SingleItem> consumer = new SingleItemConsumer();
IProcessor<SingleItem> processor = new GenericProcessor<SingleItem>(
    producer, consumer);

除此之外,这将使您的处理器更加通用,因为它现在可以处理任何生产者/消费者,即使他们不生成/消费IItem个对象。请注意生产者? extends T和消费者? super T的使用情况。因此,您可以“连接”每个生成器,它会生成某种类型的子类的实例给接受同一类型超类的实例的消费者:

IProducer<Integer> producer = new IntegerProducer();
IConsumer<Object> consumer = new ObjectConsumer();
IProcessor<Number> processor = new GenericProcessor<Number>(
    producer, consumer);