这是一个示例程序正在尝试。
我正在处理物品-单个或多个
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
的所有出现。但是,这样做的问题是我必须创建一个处理器来处理每个项目类型,这看起来似乎无法实现拥有通用“类型化”处理器的目的。
这里可能缺少明显的东西,但是,如何正确完成呢?希望能得到一些建议。
最佳答案
我认为您在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);