我试图为线形图(使用生成器模式)创建一个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()语句来创建所有对象,而不是重复多次?
类:
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(); }
}
}
一个更复杂的示例是编写类似于步骤构建器工作方式的内容。我们为每个“步骤”定义接口,并在一个类中实现所有接口。每个“切换”属性的方法实际上都返回
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();
}