The Builder Pattern is mainly used to "build a complex object in steps", where "steps" is a stable algorithm, while parts of complex objects often change. Therefore, the builder model is mainly used to solve the needs changes of the "object part". This allows for more detailed control of the object construction process.
package com.shejimoshi.create.Builder;/** * Function: The intention is to separate the construction of a complex object from its representation, so that the same construction process can create different representations* Applicability: * When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled*/public abstract class PersonBuilder{ //The tool required to draw a character is protected String tool1; protected String tool2; public PersonBuilder(String t1, String t2) { this.tool1 = t1; this.tool2 = t2; } //Drawing head public abstract void buildHead(); //Draw the body public abstract void buildBody(); //Draw the left foot and right foot public abstract void buildArmLeft(); public abstract void buildArmRight(); //Draw the left hand and right hand public abstract void buildLegLeft(); public abstract void buildLegRight();} Our abstract generator derives the corresponding subclasses to construct the corresponding actual usage type
package com.shejimoshi.create.Builder;/** * Function: The intention is to separate the construction of a complex object from its representation, so that the same construction process can create different representations* Applicability: * When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled*/public class PersonThinBuilder extends PersonBuilder{ public PersonThinBuilder(String t1, String t2) { super(t1, t2); } @Override public void buildHead() { System.out.println("Draw a thin header"); } @Override public void buildBody() { System.out.println("Draw a thin body"); } @Override public void buildArmLeft() { System.out.println("Draw the thin left arm"); } @Override public void buildArmRight() { System.out.println("Draw the thin left leg"); } @Override public void buildLegLeft() { System.out.println("Draw the thin left leg"); } @Override public void buildLegRight() { System.out.println("Draw the thin man's right leg"); }} package com.shejimoshi.create.Builder;/** * Function: The intention is to separate the construction of a complex object from its representation, so that the same construction process can create different representations* Applicability: * When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled*/public class PersonFatBuilder extends PersonBuilder{ public PersonFatBuilder(String t1, String t2) { super(t1, t2); } @Override public void buildHead() { System.out.println("Draw a fat man's head"); } @Override public void buildBody() { System.out.println("Draw a fat man's body"); } @Override public void buildArmLeft() { System.out.println("Draw a fat man's left arm"); } @Override public void buildArmRight() { System.out.println("Draw a fat man's right arm"); } @Override public void buildLegLeft() { System.out.println("Draw a fat man's left leg"); } @Override public void buildLegRight() { System.out.println("Draw the fat man's right leg"); }} Our generator is handed over to the commander to implement the created action
package com.shejimoshi.create.Builder;/** * Function: Create the commander of the character*/public class PersonDirector{ private PersonBuilder pb; //Pass the corresponding character creation model public PersonDirector(PersonBuilder pber) { this.pb = pber; } //Create a person public void createPerson() { pb.buildHead(); pb.buildBody(); pb.buildArmLeft(); pb.buildArmRight(); pb.buildLegLeft(); pb.buildLegRight(); }} Test cases:
package com.shejimoshi.create.Builder;/** * Function: Client program*/public class Test{ //Create the corresponding character public static void create(PersonBuilder pb) { //Create the corresponding object with the model passed by parameters PersonDirector pd = new PersonDirector(pb); pd.createPerson(); } public static void main(String []args) { PersonThinBuilder ptb = new PersonThinBuilder("Image Tool", "Brush"); create(ptb); //Create a thin System.out.println("===================================================================================================================================================================================================================================================================================================================================================================== Create a thin System.out.out.println("=============================================================================== Create a thin System.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out. Running results:
Draw a thin man's head, draw a thin body, draw a thin man's body, draw a thin man's left arm, draw a thin man's right arm, draw a thin man's left leg, draw a thin man's right leg, ====================================================================================================================================================================================================================== Draw a fat man's head, draw a fat man's body, draw a fat man's left arm, draw a fat man's right leg
The above is the Java Builder model, I hope it will be helpful for everyone to learn Java programming.