使用委托和枚举的步骤构建器模式?

Hexcii;

我正在从事这个项目,基本上这就是我想要实现的目标。

这就是我所拥有的:

MyObject obj = MyObject.builder()
                       .withValue("string")
                       .withAnotherValue("string")
                       .build();

MyObject obj = MyObject.builder()
                       .withValue("string")
                       .withAnotherValue("string")
                       .withField("key", "value")
                       .build();

因此,分步构建器模式会强制用户按顺序使用withValue()方法和withAnotherValue()方法。该方法field()是可选的,可以根据需要多次使用。我浏览了此网站,例如http://www.svlada.com/step-builder-pattern/

所以我想实现的是:

MyObject obj = MyObject.builder(Type.ROCK)
                       .withColour("blue")
                       .withValue("string")
                       .withAnotherValue("string")
                       .build();

MyObject obj = MyObject.builder(Type.STONE)
                       .withWeight("heavy")
                       .withValue("string")
                       .withAnotherValue("string")
                       .withField("key", "value")
                       .build();

因此,在builder()方法中,您将放置一个枚举类型,并且基于该枚举,将出现一组不同的方法。所以对于ROCK的withValue()withAnotherValue()withColour()现在是强制性的。但对于STONE withWeight()withAnotherValue()withColour()是强制性的。

我这样可能吗?在过去的两天里,我一直在尝试找出答案,但似乎无法为每种类型提供特定的方法。它仅显示了Builder中的所有方法。

任何想法和帮助将不胜感激。

码:

枚举

public enum Type implements ParameterType<Type> {

  ROCK, STONE

}

参数类型

interface ParameterType<T> {}

我的对象

public class MyObject implements Serializable {

  private static final long serialVersionUID = -4970453769180420689L;

  private List<Field> fields = new ArrayList<>();

  private MyObject() {
  }

  public interface Type {

    Value withValue(String value);
  }

  public interface Value {

    Build withAnotherValue(String anotherValue);
  }

  public interface Build {

    MyObject build();
  }

  public Type builder(Parameter type) {
    return new Builder();
  }

  public static class Builder implements Build, Type, Value {

    private final List<Field> fields = new ArrayList<>();

    @Override
    public Build withAnotherValue(String anotherValue) {
      fields.add(new Field("AnotherValue", anotherValue));
      return this;
    }

    @Override
    public Value withValue(String value) {
      fields.add(new Field("Value", value));
      return this;
    }

    @Override
    public MyObject build() {
      MyObject myObject = new MyObject();
      myObject.fields.addAll(this.fields);
      return myObject;
    }
  }

}
贾斯汀·阿尔巴诺(Justin Albano):

考虑到您正在寻找针对特定类型构建器的特定方法,因此有多个构建器,每种类型MyObject可以构建的构建器可能效果最好。您可以创建一个定义构建器的接口,然后将通用功能放入抽象类中,各个构建器都从该抽象类扩展。例如:

public interface Builder {
    public MyObject build();
}

public abstract class AbstractBuilder() {

    private final List<Field> fields = new ArrayList<>();

    protected void addField(String key, String value) {
        fields.add(new Field(key, value));
    }

    @Override
    public MyObject build() {
        MyObject myObject = new MyObject();
        myObject.fields.addAll(this.fields);
        return myObject;
    }
}

public class StoneBuilder extends AbstractBuilder {

    public StoneBuilder withValue(String value) {
        addField("Value", value);
        return this;
    }

    // ...More builder methods...
}

public class RockBuilder extends AbstractBuilder {

    public RockBuilder withAnotherValue(String value) {
        addField("AnotherValue", value);
        return this;
    }

    // ...More builder methods...
}

这使您可以MyObject通过以下方式构建实例:

MyObject obj = new RockBuilder()
                   .withValue("string")
                   .build();

MyObject obj = new StoneBuilder()
                   .withAnotherValue("string")
                   .build();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章