Skip to content

Instantly share code, notes, and snippets.

@yogeshjoshi
Last active May 17, 2020 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogeshjoshi/14b678d5d5755b70fcd1a5d402fa7ca4 to your computer and use it in GitHub Desktop.
Save yogeshjoshi/14b678d5d5755b70fcd1a5d402fa7ca4 to your computer and use it in GitHub Desktop.
Public class MyPizza{
private int size;
private boolean cheese;
private boolean pepperoni;
private boolean bacon;
private boolean veggies;
private boolean blackPepper;
public MyPizza(int size,boolean cheese, boolean pepperoni, boolean bacon, boolean veggies, boolean blackPepper){
this.size = size;
this.cheese = cheese;
this.pepperoni = pepperoni;
this.bacon = bacon;
this.veggies = veggies;
this.blackPepper = blackPepper;
}
//No Setter to Impose Immutibility Of the Object
public int getSize(){
return size;
}
public boolean isPepperoni(){
return pepperoni;
}
public boolean isBacon(){
return bacon;
}
public boolean isVeggies(){
return veggies;
}
static class Builder{
private int size;
private boolean cheese;
private boolean pepperoni;
private boolean bacon;
private boolean veggies;
private boolean blackPepper;
public Builder setSize(int size){
this.size = size;
return this;
}
public Builder setPepperoni(boolean pepperoni){
this.pepperoni = pepperoni;
return this;
}
public Builder setBacon(boolean bacon){
this.bacon = bacon;
return this;
}
public Builder setVeggies(boolean veggies){
this.veggies = veggies;
return this;
}
public Builder setBlackPepper(boolean blackPepper){
this.blackPepper = blackPepper;
return this;
}
public MyPizza build(){
return new MyPizza(size,cheese,pepperoni,bacon,veggies,blackPepper);
//See No need to remember the order of arguments in constructor, while coding you only needs to remember
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment