我尝试设计一个翻译服务器。
我的本地系统中有一个 POJO 类 (RootClass
)。并且还有远程系统将远程对象(RemoteClass
)发送到我的系统。
该服务的职责是将远程类转换为根类。
问题是:种类太多了。例如超过200种。我需要编写大量的 if-else 来完成此翻译:
我列出了一些伪代码来描述这个问题。
public class RootClass {
public String type;
public String attr1;
public String attr2;
public String attr3;
public String attr4;
}
public class RemoteClass {
public String type;
public String attr1;
public String attr2;
public String attr3;
}
public class Translator{
public RootClass translate(RemoteClass remote) {
RootClass root = new RootClass();
if ("a".equals(remote.type )) {
root.type = "veryGood";
if ("one".equals(remote.attr1)) {
root.attr2 = "true";
}
if ("two".equals(remote.attr1)) {
root.attr3 = "true";
}
if ("1".equals(remote.attr1) && "2".equals(remote.attr2) ) {
root.attr4 ="good";
}
} else if ("b".equals(remote.type)) {
root.type = "good";
if ("one".equals(remote.attr1)) {
root.attr2 = "1";
} else if ("two".equals(remote.attr1)) {
root.attr2 ="2";
}
} else if ("c".equals(remote.type)) {
root.type = "good";
if (remote.attr2.indexOf(":") > 0 ) {
String[] strArray = remote.attr2.split(":");
root.attr2=strArray[0];
root.attr3=strArray[1];
}
}
}
}
这 2 个对象描述了 1 个具有完全不同结构的事物。 根类是我们系统的核心,不可能被支持,我们也认为这个根类非常适合本地系统。对于远程类,来自第三方系统,我们无权更改。所以这个翻译变得非常困难。
我计划删除的是创建 200 多个翻译采用者: 例如:
public class adopterA implements RootAdoper {
public RootClass translate(RemoteClass remote) {
RootClass root = new RootClass();
root.type="veryGood";
if ("one".equals(remote.attr1)) {
root.attr2 = "true";
}
if ("two".equals(remote.attr1)) {
root.attr3 = "true";
}
if ("1".equals(remote.attr1) && "2".equals(remote.attr2) ) {
root.attr4 ="good";
}
}
}
并将所有这些放入 HasMap
Map<String, RootAdoper> map = new HashMap<String, RootAdoper>();
但是仍然有 200 个小类来包装 if/else,有什么好的模式或设计来解决这个复杂的问题吗?提前致谢。
最佳答案
你的 map 上的键是什么?如果 key 是 Remote.type
那么你就可以这样做
rootClass = map.get(remote.type).translate(remote);
这确实消除了 if/else if block 。只需确保处理未知/未翻译的区域或拥有不翻译或执行默认翻译的 NullObject
即可。
《Refactoring to Patterns》一书中的技术名称称为“Replace Conditional Dispatcher with Command”
不过,您仍然需要填充 map 。也许实现此目的的一种方法是将所有 RootAdoper 接口(interface)设为枚举,并将所有实现设为枚举中的类型。您还可以向枚举添加一个新方法来获取每个值可以转换的 Remote.Type。
enum RootAdoper{
A{
@Overide
public RootClass translate(RemoteClass remote){
//...
}
@Override
public String getTypeToTranslate(){
return "A";
}
},
... // other types listed here similarly
;
abstract RootClass translate(RemoteClass remote);
abstract String getTypeToTranslate();
}
然后你可以像这样填充 map
Map<String, RootAdoper> map = new HashMap<String, RootAdoper>();
for(RootAdoper adoper : RootAdoper.values(){
map.put(adoper.getTypeToTranslate(), adoper);
}
关于java - 翻译层的设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403257/