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
Java 用于转换具有嵌套列表的对象的设计模式_Java_Design Patterns_Guava - Fatal编程技术网

Java 用于转换具有嵌套列表的对象的设计模式

Java 用于转换具有嵌套列表的对象的设计模式,java,design-patterns,guava,Java,Design Patterns,Guava,我的服务流程基本上如下所示: 接收输入对象。这只是一个POJO对象,我对它的设计没有太多发言权 转换为我的服务的规范化对象 对规范化对象执行一些业务逻辑,并收集一些关于它的额外数据 转换为数据传递到的另一个服务的输出对象。(另一个POJO。) 将转换后的数据传递给另一个服务 然而,这意味着我的服务有很大一部分正在从typeInputFoo转换为typeNormalizedFoo转换为typeOutputFoo 这将是一项相当简单的任务。我正在使用Google Collections库,可以创建如

我的服务流程基本上如下所示:

  • 接收输入对象。这只是一个POJO对象,我对它的设计没有太多发言权
  • 转换为我的服务的规范化对象
  • 对规范化对象执行一些业务逻辑,并收集一些关于它的额外数据
  • 转换为数据传递到的另一个服务的输出对象。(另一个POJO。)
  • 将转换后的数据传递给另一个服务
  • 然而,这意味着我的服务有很大一部分正在从type
    InputFoo
    转换为type
    NormalizedFoo
    转换为type
    OutputFoo

    这将是一项相当简单的任务。我正在使用Google Collections库,可以创建如下类:

    public class InputFooToNormalizedFooConverter implements Function<InputFoo, NormalizedFoo> {
      public NormalizedFoo apply(InputFoo input) {
        NormalizedFoo output = new NormalizedFoo();
        output.setProperty(input.getProperty());
      }
    }
    
    public class NormalizedFooFooToOutputFooConverter implements Function<NormalizedFoo, OutputFoo> {
      public NormalizedFoo apply(InputFoo input) {
        NormalizedFoo output = new NormalizedFoo();
        output.setProperty(input.getProperty());
      }
    }
    
    在这些模型中,我可以根据每个模型的
    barId
    计算出哪个
    Baz
    qux
    属于哪个
    Bar


    此时,我有大约20个不同的转换器,用于从
    输入
    标准化
    标准化
    输出
    。更糟糕的是,它们中的一些名称类似于
    ReallyLongInputTypeToReallyLongNormalizedTypeConverter
    创建非常长的类名。我觉得我在这里做错了什么,所有的转换器。有没有更好的方法来组织我的转换器?

    接口没有成员变量。这很公平,但我认为Foo类是接口还是类对示例来说并不重要,因为我没有更改它的权限。我不知道形状。也许像Orika这样的bean映射框架可以减轻一些痛苦?是的,我们过去常常使用对象映射,但Orika看起来很整洁。此外,我不确定使用Guava函数是否总是正确的方法,命令式代码有时可能更简单(正如他们在常见问题解答中解释的)。
    public class Foo {
       List<Bar> barItems;
       // .. other properties
    }
    
    public class Bar {
       List<Baz> bazItems;
       List<Quux> quuxItems;
       // .. other properties
    }
    
    public class Baz {
       // .. other properties
    }
    
    public class Quux {
       // .. other properties
    }
    
    public class InputFoo {
      public List<InputBar> bars;
      public List<InputBaz> bazs;
      public List<InputQuux> quuxs;
      // .. other properties
    }
    
    public class InputBar {
      private String barId;
      // .. other properties
    }
    
    public class InputBaz {
      private String barId;
      private String bazId;
      // .. other properties
    }
    
    public class InputQuux {
      private String barId;
      private String quuxId;
      // .. other properties
    }