I. Definition:

The Builder pattern separates the construction and presentation of a complex object so that the same construction process can create different representations.

UML structure diagram of builder pattern:

The Builder mode consists of four main characters:

-Blair: You’re an abstract Builder. It declares the abstract interface specified for the parts that create a Product object. ConcreteBuilder: a ConcreteBuilder that implements the Builder abstract interface, builds and assembles parts, defines and specifies the representation it creates, and provides an interface to retrieve products. I’m the Director. Build an object that uses the Builder interface. It is mainly used to create a complex object, it has two main functions, one is: the isolation of customer and object production process, the other is: responsible for controlling the production process of product object. Product: Product role. Represents a complex object being constructed. The ConcreteBuilder creates an internal representation of the product and defines its assembly process, including classes that define the components, including interfaces that assemble them into the final product.

 

Ii. Realization of the mode:

KFC generally has several kinds of set meals for customers to choose. It can make these set meals in the future according to the set meals ordered by customers and return them with a complete and beautiful set meal. Next, we will simulate this process. We agree that the set meal mainly consists of hamburger, French fries, Coke, chicken legs and other components. Different sets can be constructed by using different components.

The first is set meals:

public class Meal {
    private String food;
    private String drink;
 
    public String getFood(a) {
        return food;
    }
 
    public void setFood(String food) {
        this.food = food;
    }
 
    public String getDrink(a) {
        return drink;
    }
 
    public void setDrink(String drink) {
        this.drink = drink; }}Copy the code

Package constructor:

public abstract class MealBuilder {
    Meal meal = new Meal();
    
    public abstract void buildFood(a);
    
    public abstract void buildDrink(a);
    
    public Meal getMeal(a){
        returnmeal; }}Copy the code

A, B. Both packages implement abstract package classes:

 
Copy the code
public class MealA extends MealBuilder{
 
    public void buildDrink(a) {
        meal.setDrink("A Coke");
    }
 
    public void buildFood(a) {
        meal.setFood("A box of chips."); }}Copy the code
public class MealB extends MealBuilder{
 
    public void buildDrink(a) {
        meal.setDrink("A glass of lemon juice.");
    }
 
    public void buildFood(a) {
        meal.setFood("Three wings"); }}Copy the code

Finally, there is the KFC waiter, who is like a conductor, who decides the process of realizing the meal and then gives you a perfect meal.

public class KFCWaiter {
    private MealBuilder mealBuilder;
    
    public void setMealBuilder(MealBuilder mealBuilder) {
        this.mealBuilder = mealBuilder;
    }
 
    public Meal construct(a){
        // Prepare the food
        mealBuilder.buildFood();
        // Prepare drinks
        mealBuilder.buildDrink();
        
        // When ready, return a complete package to the customer
        returnmealBuilder.getMeal(); }}Copy the code

The test class:

public class Client {
    public static void main(String[] args) {
        / / the waiter
        KFCWaiter waiter = new KFCWaiter();
        / / set A
        MealA a = new MealA();
        // The waiter prepares set meal A
        waiter.setMealBuilder(a);
        // Get the package
        Meal mealA = waiter.construct();
        
        System.out.print("Components of Package A :");
        System.out.println(mealA.getFood()+"-"+mealA.getDrink()); }}Copy the code

Running results:

The ingredients of meal A: A box of French fries and A cokeCopy the code

 

Iii. Advantages and disadvantages of the mode:

1. Advantages:

(1) The creation steps of complex products are decomposed into different methods, which makes the creation process clearer and enables us to control the generation process of complex objects more accurately.

(2) Separate the product creation process from the product itself, and different products can be obtained by using the same creation process. That is, detail depends on abstraction.

(3) Each specific builder is relatively independent, and has nothing to do with other specific builders, so it can be very convenient to replace specific builders or add new specific builders, users can use different specific builders to get different product objects.

2. Disadvantages:

(1) The products created by the Builder mode generally have more in common and their components are similar. If there is a large difference between the products, the builder mode is not suitable for use, so its application scope is limited to a certain extent.

(2) If the internal changes of the product are complex, it may result in the need to define many specific builder classes to implement such changes, resulting in the system becoming very large.

3. Applicable Scenarios:

(1) The product objects that need to be generated have complex internal structures. These product objects usually contain multiple member attributes, when the algorithm to create complex objects should be independent of the components of the object and their assembly method.

(2) Isolate the creation and use of complex objects, and enable the same creation process to create different products.

 

The differences between abstract Factory mode and builder mode:

1, abstract factory model to achieve the creation of a product family, a product family is such a series of products: product combinations with different classification dimensions, the use of abstract factory model is not concerned with the construction process, only what products by what factory can be produced. While the builder mode is required to build products according to the specified blueprint, its main purpose is to produce a new product by assembling parts, the difference between the two is quite obvious.

2, we use the “factory” in the abstract factory pattern to describe the builder, the builder pattern used in the “shop” to describe the builder, in fact we have been talking them the difference between the two, the abstract factory pattern is like a factory, a BMW factory VAN BMW SUV and BMW, Mercedes factory production Mercedes Benz car SUV and VAN, It looks at the construction of objects from a higher level. Specifically, there are many workshops inside the factory, such as the workshop for manufacturing engines and assembling engines, etc., but these are hidden details inside the factory and are not publicized to the public. That is, for a leader, he only cares about what products a factory produces, not how.

3, and the builder pattern is different, it is composed of workshop, create and assembly of different workshop to complete different tasks, a complete car production process need engine manufacturing workshop, the matching of the engine assembly shop to complete, they cooperate is the basis of the design blueprint, and it is the blueprint for the hands of the workshop Director (Director), It gives the builder the blueprint to produce the product, and the builder model is more concerned with the building process. Although a workshop still produces vehicles from the outside, the transformation of this workshop is very fast. As long as a blueprint is redesigned, different products can be produced, which depends on the contribution of the builder model.

4. The Abstract Factory model is relatively larger in scale than the Builder model. It focuses on the product as a whole, whereas the Builder model focuses on the build process. Because of this, the two scenarios are so different that if you want to mask the creation of an object and provide only a well-wrapped object, you can choose the abstract Factory method pattern. The builder pattern can be used in the assembly of components, for example, by assembling different components or different sequences of the same components, a new object can be generated, which can produce a very flexible architecture, and easily extend and maintain the system.

Blog.csdn.net/chenssy/art…