gpt4 book ai didi

java - Java 中的嵌套构建器模式类

转载 作者:行者123 更新时间:2023-11-30 07:26:53 25 4
gpt4 key购买 nike

我正在尝试为线图创建一个java类(使用构建器模式),其中包含许多属性。基于这些属性的应用,我尝试创建内部构建器类(例如 X 轴属性、Y 轴属性等)。我是 Java 新手,我想知道是否可以像下面的示例一样调用构建器对象。我点击了链接Can the builder pattern ever be doing too much? 。它将以增量方式构建对象。我喜欢这个主意。但是,我不知道如何将其用于我的场景。

调用方法:

new LineGraph().UiPropBuilder(ctx)
.setBackgroundColor(Color.BLUE)
.build()
.XAxisProperties()
.enableGridLines(true)
.build()
.YAxisProperties()
.enableGridLines(false)
.build();

是否可以仅调用 .build() 语句一次来创建所有对象,而不是重复多次?

类(class):

public class LineGraph{

private LineGraph()

public static class UiPropBuilder{

private Integer mBackgroundColor;
private Boolean bTouchEnabled;
...

public UiPropBuilder (Context ctx) { this.ctx = ctx; }

public UiPropBuilder setBackgroundColor(Integer mBackgroundColor){ this.mBackgroundColor = mBackgroundColor; return this;}

public UiPropBuilder touchEnabled(Boolean bTouchEnabled){ this.bTouchEnabled = bTouchEnabled; return this;}

... some more properties...
public UiPropBuilder build(){ return new UiPropBuilder(this); }

}

// X-Axis properties builder
public static class XAxisProperties{
// variable declarations and constructor omitted
public XAxisProperties enableGridLines(Boolean enable) {this.enable = enable); return this;}
public XAxisProperties build(){ return new XAxisProperties(this);}


// Y-Axis properties Builder

public static class YAxisProperties{
// variable declarations and constructor omitted
public YAxisProperties enableGridLines(Boolean enable) {this.enable = enable); return this;}
public YAxisProperties build(){ return new YAxisProperties(this);}
}

最佳答案

一种方法是这样,您有 3 个构建器对象,它们都可以互相引用并在它们之间切换。这与您已经尝试做的事情类似:

public static class UiPropBuilder {
private XAxisProperties xAxisProperties = new XAxisProperties();
private YAxisProperties yAxisProperties = new YAxisProperties();
// ...
public XAxisProperties xAxisProperties() { return xAxisProperties; }
public YAxisProperties yAxisProperties() { return yAxisProperties; }

public LineGraph build() {
return new LineGraph(..., ..., ..., ...);
}

public class XAxisProperties {
// ...
public YAxisProperties yAxisProperties() { yAxisProperties; }
public UiPropBuilder uiProperties() { return UiPropBuilder.this; }
public LineGraph build() { return UiPropBuilder.this.build(); }
}

public class YAxisProperties {
// ...
public XAxisProperties xAxisProperties() { return xAxisProperties; }
public UiPropBuilder uiProperties() { return UiPropBuilder.this; }
public LineGraph build() { return UiPropBuilder.this.build(); }
}
}

一个更复杂的示例是编写类似于步骤构建器工作方式的代码。我们为每个“步骤”定义接口(interface),并在单个类中实现所有这些接口(interface)。每个“切换”属性的方法实际上都会返回 this

public interface BuildStep       { LineGraph        build();           }
public interface UiSwitchStep { UiPropBuilder uiProperties(); }
public interface XAxisSwitchStep { XAxisPropBuilder xAxisProperties(); }
public interface YAxisSwitchStep { YAxisPropBuilder yAxisProperties(); }

public interface UiPropBuilder
extends XAxisSwitchStep, YAxisSwitchStep, BuildStep {
// UI property setters
// ...
}

public interface XAxisPropBuilder
extends YAxisSwitchStep, UiSwitchStep, BuildStep {
// X-axis property setters
// ...
}

public interface YAxisPropBuilder
extends XAxisSwitchStep, UiSwitchStep, BuildStep {
// Y-axis property setters
// ...
}

private static class UiBuilderImpl
implements UiPropBuilder, XAxisPropBuilder, YAxisPropBuilder {
// implement ALL property setters
// ...

@Override
public XAxisProperties xAxisProperties() {
return this;
}
@Override
public YAxisProperties yAxisProperties() {
return this;
}
@Override
public UiPropBuilder uiProperties() {
return this;
}
@Override
public LineGraph build() {
return new LineGraph(..., ..., ..., ...);
}
}

// create new builders through a static method
// which returns the interface type
public static UiPropBuilder builder() {
return new UiBuilderImpl();
}

关于java - Java 中的嵌套构建器模式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36706799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com