我今天早上问similar question,以为我已经回答了,实际上实施了建议的解决方案,但没有完成我需要的工作.所以这是我的全部问题:
我有以下课程:
// Objects to process.
public class Apple {
private Color color;
private int numberOfSeeds;
// ...
}
public class Chair {
private Material madeOutOfMaterial;
private double price;
// ...
}
public class CellPhone {
private Manufacturer make;
private String model;
private boolean isSmartPhone;
// ...
}
// Contains the object that will be processed (an Apple, Chair, CellPhone instance, etc.)
// as well as metadata info about the processing itself (timestamp, a UUID for tracking
// purposes, etc.).
public class ProcessingMetadata {
private PROCESSABLE_OBJECT result;
private Date processedOnDate;
private String uuid;
// ...
}
// Abstract base class for processing PROCESSABLE_OBJECTs.
public abstract class ObjectProcessor> {
private String name;
public abstract ProcessingMetadata process(Data data);
}
// One concrete processor.
public class SimpleObjectProcessor extends ObjectProcessor> {
private Fizz fizz;
@Override
public ProcessingMetadata process(Data data) {
// Processes data one way, and returns a PROCESSABLE_OBJECT.
}
}
// Another concrete processor.
public class ComplexObjectProcessor extends ObjectProcessor> {
private Buzz buzz;
@Override
public ProcessingMetadata process(Data data) {
// Processes data differently, and returns a PROCESSABLE_OBJECT.
}
}
这样,使用所有这些类的最终代码如下所示:
ObjectProcessor> appleProcessor =
new ComplexObjectProcessor>();
Data data = getData();
ProcessingMetadata meta = appleProcessor.process(data);
Apple apple = meta.getResult();
Date processedOn = meta.getProcessedOnDate();
String uuid = meta.getUUID();
这是我要向开发人员公开的“ API”.获取一些数据,选择类型处理器,处理数据,并获取所需的所有元数据.
问题是我的ObjectProcessor< ProcessingMetadata< PROCESSABLE_OBJECT>不合法在类定义的ProcessingMetadata部分上,它为我提供了以下编译器错误:
Syntax error on token(s), misplaced construct(s)
我整个上午都在玩这个类(及其具体的子类),似乎无法正确设置泛型来提供所需的API.我愿意要求PROCESSABLE_OBJECTs实际上是一个接口,例如Processable,并让Apple,Chair等实现它.但是并没有告知客户要处理的内容,因此,我尽可能不要强迫他们让其POJO实现Processabl接口.
那么,我想要的API是否可能?如果是这样,怎么办?如果没有,那为什么呢,我能得到的最接近的是什么?提前致谢!