Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 对象转换的设计模式_Design Patterns - Fatal编程技术网

Design patterns 对象转换的设计模式

Design patterns 对象转换的设计模式,design-patterns,Design Patterns,我正在寻找以下场景中使用的最佳模式的建议 涉及从域级对象转换为UI DTO 对于所讨论的对象,BeanUtils或PropertyUtils将执行大部分转换 我的域级对象包含一个“type”属性。基于此类型,我们需要将DTO“label”属性设置为友好名称 因此,对于AAA类型,我们需要创建如下标签:property1+“-”+property2 对于BBB类型,我们需要创建如下标签:property2+“/”+property1 对于CCC类型,我们需要基于数据库查询创建标签 (上述逻辑过

我正在寻找以下场景中使用的最佳模式的建议

涉及从域级对象转换为UI DTO

对于所讨论的对象,
BeanUtils
PropertyUtils
将执行大部分转换

我的域级对象包含一个“type”属性。基于此类型,我们需要将DTO“label”属性设置为友好名称

  • 因此,对于AAA类型,我们需要创建如下标签:
    property1+“-”+property2
  • 对于BBB类型,我们需要创建如下标签:
    property2+“/”+property1
  • 对于CCC类型,我们需要基于数据库查询创建标签
(上述逻辑过于简单化)

在没有研究所有可能的设计模式的情况下,我想到了以下模式:

  • 创建多个LabelCreationStrategies,每种类型一个
  • 在我的对象转换助手中,插入一个映射“类型”到策略的映射
  • 因此,当对象转换发生时,它将找到 适当的策略,并创建最合适的标签
  • 还有什么其他模式可以在这里工作


    谢谢

    使用策略模式

     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)都知道如何生成其策略。@杰夫:您可以将此作为答案发布。