I am trying to create a java class for a line graph (using builder pattern) that encloses many properties within. Based on the application of these properties, I am trying to create inner builder classes (such as X-Axis Properties, Y-Axis properties etc). I am new to Java and I would like to know if it is possible to invoke builder objects like below in my example. I followed through a link Can the builder pattern ever be doing too much?. that would build objects in increments. I liked the idea. However, I do not know how could I put it to use for my scenario.
Invoke method:
new LineGraph().UiPropBuilder(ctx)
.setBackgroundColor(Color.BLUE)
.build()
.XAxisProperties()
.enableGridLines(true)
.build()
.YAxisProperties()
.enableGridLines(false)
.build();
Is it possible to call .build() statement only once to create all objects instead of repeating it multiple times?
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);}
}