Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 Builder模式时的模块化?_Java_Design Patterns - Fatal编程技术网

使用Java Builder模式时的模块化?

使用Java Builder模式时的模块化?,java,design-patterns,Java,Design Patterns,我按照创建了一个类,它对我来说非常有用,但是现在我发现自己需要更多的模块化。这可以在不报废建筑商的情况下完成吗 例如,我希望使用枚举的配置数组(或任何其他允许我自定义字段顺序和选择的方法)构建Person对象。这些值来自各种嵌套对象,我需要在特定时间以特定顺序使用特定字段来创建输出CSV public static enum FIELDS = { FIRST, MIDDLE, LAST } List<FIELDS> fields = { FIRST, LAST } creates:

我按照创建了一个类,它对我来说非常有用,但是现在我发现自己需要更多的模块化。这可以在不报废建筑商的情况下完成吗

例如,我希望使用枚举的配置数组(或任何其他允许我自定义字段顺序和选择的方法)构建Person对象。这些值来自各种嵌套对象,我需要在特定时间以特定顺序使用特定字段来创建输出CSV

public static enum FIELDS = { FIRST, MIDDLE, LAST }

List<FIELDS> fields = { FIRST, LAST } creates:
Person person = new Person.builder().first("john").last("doe").build();

List<FIELDS> fields = { LAST, FIRST, MIDDLE } creates:
Person person = new Person.builder().last("doe").first("john").middle("q").build();
公共静态枚举字段={FIRST,MIDDLE,LAST}
列表字段={FIRST,LAST}创建:
Person-Person=newperson.builder().first(“john”).last(“doe”).build();
列表字段={LAST,FIRST,MIDDLE}创建:
Person-Person=newperson.builder().last(“doe”).first(“john”).middle(“q”).build();

是否可以在枚举上执行任何类型的switch语句来动态构造这样的人?

这项工作可能应该在
enum
本身内部完成。您可以在主
enum
正文中声明
abstract
方法,然后在每个
enum
元素中重写它

import java.util.Iterator;

public class Person {
  public static enum FIELDS {
    FIRST ("john") { protected void addString(PersonBuilder builder) { builder.first(getName()); } },
    MIDDLE ("q") { protected void addString(PersonBuilder builder) { builder.middle(getName()); } },
    LAST ("doe") { protected void addString(PersonBuilder builder) { builder.last(getName()); } };

    private String name;

    protected abstract void addString(PersonBuilder builder);

    private FIELDS(String name) {
      this.name = name;
    }

    protected String getName() {
      return name;
    }
  }

  public static class PersonBuilder {
    public PersonBuilder addIterable(Iterable<FIELDS> iter) {
      Iterator<FIELDS> it = iter.iterator();
      while(it.hasNext()) {
        it.next().addString(this);
      }

      return this;
    }

    // you wrote these already, this is just to make it compile
    public void first(String first) { }
    public void middle(String middle) { }
    public void last(String last) { }
  }
}
import java.util.Iterator;
公共阶层人士{
公共静态枚举字段{
FIRST(“john”){protectedvoidaddstring(PersonBuilder){builder.FIRST(getName());}},
MIDDLE(“q”){protectedvoidaddstring(PersonBuilder){builder.MIDDLE(getName());}},
LAST(“doe”){protectedvoidaddstring(PersonBuilder){builder.LAST(getName());};
私有字符串名称;
受保护的抽象void addString(PersonBuilder);
专用字段(字符串名称){
this.name=名称;
}
受保护的字符串getName(){
返回名称;
}
}
公共静态类PersonBuilder{
公共人员生成器可添加(Iterable iter){
迭代器it=iter.Iterator();
while(it.hasNext()){
it.next().addString(this);
}
归还这个;
}
//你已经写了这些,这只是为了让它编译
public void first(字符串first){}
公共空中间(字符串中间){}
公共void last(字符串last){}
}
}

你可以这样做;e、 g

   public Person list2Person(List<FIELDS> fields) {
       Builder builder = new Person.builder();
       for (Field field : fields) {
           switch (field) {
           case FIRST:
              builder.first(getValue(FIRST));
              break;
           case MIDDLE:
              builder.middle(getValue(MIDDLE));
              break;
           ...
           }
           return builder.build();
       }
   }
公众人物列表2个人(列表字段){
Builder=newperson.Builder();
用于(字段:字段){
开关(现场){
案例一:
builder.first(getValue(first));
打破
案例中:
builder.middle(getValue(middle));
打破
...
}
返回builder.build();
}
}

当然,这假设您有一些方法来实现
getValue(FIELDS)

,所以您需要使用枚举的配置数组来创建实例?或者你想有几个有特定约束的构建器变体?@TheTerriblesWifta是一个枚举的配置数组(或任何等效的方式),更新了问题使其更清晰好吧,但还有一件事不清楚。如果只有枚举映射到字段,您希望从何处获取值?@theterribleswiftheh再次更新,值来自csv,并传递给构造生成器的方法。这是一个简化的版本,真正的版本有对象列表等等,而不是简单的枚举->字段名映射。如果你从文件加载数据,构建器的意义是什么,它是用于源代码级创建的?哇,我想我真的想得太多了,这正是我需要的。谢谢斯蒂芬!