Am I still following the Builder pattern with the implementation below? What's confusing me is the protected constructor in the class "myClass". My first thought when it comes to the builder pattern is to hide the object that the builder is supposed to construct. But here we don't. I can't understand if this is just bad design or ok design.
public class MyClass{
private final String x;
private final String y;
protected MyClass(MyBuilder builder){
this.x = builder.getX();
this.y = builder.getY();
}
//getters...
}
public class MyBuilder{
private String X;
private String Y;
public MyBuilder withX(String x){
this.x = x;
return this;
}
public MyBuilder withY(String y){
this.y = y;
return this;
}
public MyClass build(){
return new MyClass(this);
}
//getters....
}
public class Main{
public static void main(){
//Example 1
MyClass myClass = new MyBuilder().withX("x").withY("y").build();
//Example 2
MyClass myClass2 = new MyClass(new MyBuilder().withX("x").withY("y"));
}
}