[英]Java generics software engineering design
I have a class RabbitQueue which basically acts like a queue and implements my Pollable interface. 我有一个RabbitQueue类,它基本上像一个队列,并实现了我的Pollable接口。
I also have a class SaveToDatabaseStrategy which implements my DataProcessingStrategy interface. 我还有一个类SaveToDatabaseStrategy ,它实现了我的DataProcessingStrategy接口。 This is designed following the strategy-pattern. 这是根据策略模式设计的。
Now, my class InputHandler which implements my interface InputListener, contains an instance of the Pollable interface and one of the DataProcessingStrategy interface. 现在,实现我的接口InputListener的类InputHandler包含Pollable接口的一个实例和DataProcessingStrategy接口之一。
However, I don't want to set the Generic type (String) when I declare these two fields, since the Generic type depends on the implementation of this interface which is given later on. 但是,在声明这两个字段时,我不想设置通用类型(String),因为通用类型取决于此接口的实现,这将在以后给出。
How would you design this? 您将如何设计?
public interface Pollable<T> {
T poll();
}
public class RabbitQueue implements Pollable<String> {
// code..
}
public interface DataProcessingStrategy<T> {
void processData(T t);
}
public class SaveToDatabaseStrategy<T> implements DataProcessingStrategy<T> {
private Repository<T, ?> repo;
public SaveToDatabaseStrategy(Repository<T, ?> repo) {
this.repo = repo;
}
@Override
public void processData(T data) {
repo.create(data);
System.out.printf("Received data of type %s: %s\n", data.getClass().getSimpleName(), data);
}
}
public interface InputListener<T> {
void inputReceived();
void inputReceived(T t);
}
public class InputHandler implements InputListener<String> {
private Pollable<String> queue;
private DataProcessingStrategy<String> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
@Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
@Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll());
}
}
You could add a type parameter to the InputHandler class. 您可以将类型参数添加到InputHandler类。
public class InputHandler<T> implements InputListener<T> {
private Pollable<T> queue;
private DataProcessingStrategy<T> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
@Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
@Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll().toString());
}
}
Then create a new object like 然后创建一个新的对象,如
new InputHandler<String>(host, queueName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.