The builder pattern, also known as the generator pattern, is a relatively complex and relatively infrequently used creation pattern. The builder pattern returns not a simple product to the client, but a complex product consisting of multiple parts.
My own understanding: The builder can create a complex product class and return a product instance with the properties set. The user only needs to tell the commander which complex product he wants (pass in a concrete builder to the commander, and the commander will abstract according to the abstraction. Builder programming), the commander calls the create and return methods declared in the abstract builder.
● Abstract Builder: It specifies an abstract interface for creating each component of a product object, in which two types of methods are generally declared, one type of method is used to create each component of a complex object; the other type of method is used to return complex objects. It can be either an abstract class or an interface.
●Concrete builder: It implements the abstract builder interface, realizes the concrete construction and assembly methods of each component, defines and clarifies the complex objects it creates, and can also provide a method to return the created complex product objects.
● Product Role: It is a complex object that is constructed, containing multiple components, and the specific builder creates an internal representation of the product and defines its assembly process.
● Commander: The conductor is also known as the director class. It is responsible for arranging the construction order of complex objects. There is an association between the conductor and the abstract builder. The component construction and assembly methods of the builder object can be called in its construction method. Complete the construction of complex objects. The client generally only needs to interact with the conductor, determine the type of the specific builder on the client, and instantiate the specific builder object (also through the configuration file and reflection mechanism), and then pass the constructor or Setter method of the conductor class Pass this object into the conductor class.
Product role:
// complex product public class Actor { private String type; //role type private String sex; //gender private String face; //face shape private String costume; //Clothing private String hairstyle; // hairstyle } //Omit Getter and Setter methodsAbstract Builder:
// character builder public abstract class ActorBuilder { protected Actor actor = new Actor(); public abstract void buildType(); public abstract void buildSex(); public abstract void buildFace(); public abstract void buildCostume(); public abstract void buildHairstyle(); //Factory method, returns a complete game character object public Actor createActor() { return actor; } }
Specific builder:
public class AngelBuilder extends ActorBuilder { public void buildType() { actor.setType("天使"); } public void buildSex() { actor.setSex("女"); } public void buildFace() { actor.setFace("Pretty"); } public void buildCostume() { actor.setCostume("white dress"); } public void buildHairstyle() { actor.setHairstyle("Shawl Long Hair"); } }
public class DevilBuilder extends ActorBuilder{ public void buildType() { actor.setType("Demon"); } public void buildSex() { actor.setSex("妖"); } public void buildFace() { actor.setFace("ugly"); } public void buildCostume() { actor.setCostume("Black"); } public void buildHairstyle() { actor.setHairstyle("bald head"); } }
Commander:
//Game character creation controller: conductor class ActorController { // Build complex product objects step by step public Actor construct(ActorBuilder ab) { actor actor; ab.buildType(); ab.buildSex(); ab.buildFace(); ab.buildCostume(); ab.buildHairstyle(); actor=ab.createActor(); return actor; } }
Client:
class Client { public static void main(String args[]) { //declare the required type ActorBuilder ab = new DevilBuilder(); //create the conductor ActorController ac = new ActorController(); Actor actor = ac.construct(ab); //Create a complete builder object through the conductor System.out.println(actor.toString()); } }