多くのプロパティを囲む折れ線グラフ(ビルダーパターンを使用)のJavaクラスを作成しようとしています。これらのプロパティのアプリケーションに基づいて、内部ビルダークラス(X軸プロパティ、Y軸プロパティなど)を作成しようとしています。私はJavaに慣れていないので、私の例では以下のようなBuilderオブジェクトを呼び出すことができるかどうかを知りたいと思っています。私はリンクCan the builder pattern ever be doing too much?に続いた。インクリメントでオブジェクトを構築します。私はそのアイディアが好きだった。しかし、私は自分のシナリオでどのように使うことができるのか分かりません。Javaでビルダーパターンクラスをネストする
Invokeメソッド:
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);}
}
あなたの質問では、 "ビルド(ビルド)文はすべてのオブジェクトを作成するために1回だけです。"あなたは "ビルドするすべてのプロパティを作成するために.build()ステートメントを1回だけ呼び出す"という記述をしていましたか? – alphablue