Design patterns 对象转换的设计模式
我正在寻找以下场景中使用的最佳模式的建议 涉及从域级对象转换为UI DTO 对于所讨论的对象,Design patterns 对象转换的设计模式,design-patterns,Design Patterns,我正在寻找以下场景中使用的最佳模式的建议 涉及从域级对象转换为UI DTO 对于所讨论的对象,BeanUtils或PropertyUtils将执行大部分转换 我的域级对象包含一个“type”属性。基于此类型,我们需要将DTO“label”属性设置为友好名称 因此,对于AAA类型,我们需要创建如下标签:property1+“-”+property2 对于BBB类型,我们需要创建如下标签:property2+“/”+property1 对于CCC类型,我们需要基于数据库查询创建标签 (上述逻辑过
BeanUtils
或PropertyUtils
将执行大部分转换
我的域级对象包含一个“type”属性。基于此类型,我们需要将DTO“label”属性设置为友好名称
- 因此,对于AAA类型,我们需要创建如下标签:
property1+“-”+property2
- 对于BBB类型,我们需要创建如下标签:
property2+“/”+property1
- 对于CCC类型,我们需要基于数据库查询创建标签
谢谢使用策略模式
public abstract class AbstractClass{ //or interface
public abstract void print();
}
public class AAA extends AbstractClass{
@Override
public void print(){
System.out.println("AAA");
}
}
public class BBB extends AbstractClass{
@Override
public void print(){
System.out.println("BBB");
}
}
您可以将类型更改为知道如何生成适当策略的实际对象 例如:
public interface Type {
NamingStrategy getNamingStrategy();
}
public class AAA implements Type {
public NamingStrategy getNamingStrategy() {
return new DatabaseQueryStrategy();
}
public String toString() {
return "AAA";
}
}
您还可以将类型设置为多态对象,其中每个类型(AAA、BBB)都知道如何生成其策略。@杰夫:您可以将此作为答案发布。