[英]How to get the correct implementation by condition
I have three classes of messages.我有三类消息。
public class ByteMessage extends Message {
private byte[] message;
private long size;
public ByteMessage() {
this.setReceiverType(ReceiverType.FILE);
}
//getters and setters
}
public class CharMessage extends Message {
private char[] message;
public CharMessage() {
this.setReceiverType(ReceiverType.FILE);
}
}
public class TextMessage extends Message {
private String message;
public TextMessage() {
this.setReceiverType(ReceiverType.DB);
}
//getters and setters
}
They are inherited from the message class Message
.它们继承自消息类
Message
。
public class Message {
private ReceiverType receiverType;
public ReceiverType getReceiverType() {
return receiverType;
}
public void setReceiverType(ReceiverType receiverType) {
this.receiverType = receiverType;
}
}
The class has a field receiverType
that defines where the message will be written.该类有一个字段
receiverType
,用于定义消息的写入位置。 And now I have two receivers where a message can be recorded: DbReceiver and FileReceiver:现在我有两个可以记录消息的接收器:DbReceiver 和 FileReceiver:
public class DbReceiver implements Receiver {
@Override
public void writeMessage(Message message) {
//do something
}
}
public class FileReceiver implements Receiver {
@Override
public void writeMessage(Message message) {
// do something
}
}
And I have a service that processes messages and which should, depending on the type of message, cause one or another implementation.我有一个处理消息的服务,它应该根据消息的类型导致一个或另一个实现。 How can I do this - get the right implementation?
我该怎么做 - 获得正确的实施?
public class Service {
public void handleMessage(Message message) {
Receiver receiver = ???
receiver.writeMessage(message);
}
}
I know the solution with 'Map', but it doesn't look too good for me.我知道“地图”的解决方案,但对我来说看起来不太好。 Perhaps there are other solutions?
也许还有其他解决方案?
public class Service {
@Autowired
private Map<ReceiverType, Receiver> receiverMap;
public void handleMessage(Message message) {
Receiver receiver = getReceiver(message.getReceiverType());
receiver.writeMessage(message);
}
private Receiver getReceiver(ReceiverType receiverType){
return receiverMap.get(receiverType);
}
}
It is possible to provide the instance of the receiver in the ReceiverType
enum:可以在
ReceiverType
枚举中提供接收器的实例:
Update: Implementations of the receiver may be injected into enum values using special @Component
with autowired receivers and @PostConstruct
method:更新:接收器的实现可以使用带有自动装配接收器的特殊
@Component
和@PostConstruct
方法注入到枚举值中:
public enum ReceiverType {
DB,
FILE;
private Receiver receiver = null;
private void setReceiver(Receiver receiver) {
if (null == this.receiver && null != receiver) {
this.receiver = receiver;
}
}
public Receiver getReceiver() {
return this.receiver;
}
@Component
public static class ReceiverInjector {
@Autowired
private DbReceiver db;
@Autowired
private FileReceiver file;
@PostConstruct
public void postConstruct() {
ReceiverType.DB.setReceiver(db);
ReceiverType.FILE.setReceiver(file);
}
}
}
Receiver implementations should be marked with @Component
annotation too:接收器实现也应该用
@Component
注释标记:
@Component
public class DbReceiver implements Receiver {
@Override
public void writeMessage(Message message) {
//do something
}
}
@Component
public class FileReceiver implements Receiver {
@Override
public void writeMessage(Message message) {
// do something
}
}
Then the service may be implemented as simply as follows:那么该服务可以简单地实现如下:
public class Service {
public void handleMessage(Message message) {
message.getReceiverType().getReceiver().writeMessage(message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.