11. Design Pattern Builder Pattern (Builder Pattern) Take building a car and buying a car as an example

Builder mode

Definition: Separate the construction of a complex object from its representation, so that the same construction process can create different representations.

Type: Create class pattern

Class Diagram:
Insert picture description here

Four elements

  1. Product category: Generally, it is a more complex object, which means that the process of creating an object is more complicated, and generally there will be more code. In this class diagram, the product class is a concrete class, not an abstract class. In actual programming, a product class can be composed of an abstract class and its different implementations, or it can be composed of multiple abstract classes and their implementations.
  2. Abstract builder: The purpose of introducing abstract builder is to hand over the specific process of construction to its subclasses to achieve. This is easier to expand. There are generally at least two abstract methods, one for building products and one for returning products.
  3. Builder: To implement all the unimplemented methods of the abstract class. Specifically, it is generally two tasks: build a product; return the built product.
  4. Director class: Responsible for calling the appropriate builder to build the product. The director class generally does not depend on the product class. It is the builder class that directly interacts with the director class. Generally speaking, the director class is used to encapsulate the changeable part of the program.
    Code
    class Product {
        private String name;
        private String type;
        public void showProduct(){
            System.out.println("名称:"+name);
            System.out.println("型号:"+type);
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setType(String type) {
            this.type = type;
        }
    }

    abstract class Builder {
        public abstract void setPart(String arg1, String arg2);
        public abstract Product getProduct();
    }
    class ConcreteBuilder extends Builder {
        private Product product = new Product();

        public Product getProduct() {
            return product;
        }

        public void setPart(String arg1, String arg2) {
            product.setName(arg1);
            product.setType(arg2);
        }
    }

    public class Director {
        private Builder builder = new ConcreteBuilder();
        public Product getAProduct(){
            builder.setPart("宝马汽车","X7");
            return builder.getProduct();
        }
        public Product getBProduct(){
            builder.setPart("奥迪汽车","Q5");
            return builder.getProduct();
        }
    }
    public class Client {
        public static void main(String[] args){
            Director director = new Director();
            Product product1 = director.getAProduct();
            product1.showProduct();

            Product product2 = director.getBProduct();
            product2.showProduct();
        }
    }

Advantages of builder mode

First of all, the encapsulation of the builder mode is very good. Using the builder mode can effectively encapsulate changes. In the scenario where the builder mode is used, the general product category and the builder category are relatively stable. Therefore, encapsulating the main business logic in the director category can be compared as a whole Good stability.

Second, the builder mode is easy to expand. If there is a new requirement, it can be completed by implementing a new builder class. Basically, there is no need to modify the code that has been tested before, so it will not introduce risks to the original function.

The difference between builder mode and factory mode

We can see that the builder mode is very similar to the factory mode. Overall, the builder mode only has one more "director-like" role than the factory mode. In the class diagram of the builder pattern, if the director class is regarded as the client that is finally called, the rest of the picture can be regarded as a simple factory pattern.

Compared with the factory mode, the builder mode is generally used to create more complex objects, because the object creation process is more complicated, so the object creation process is separated into a new class-director class. In other words, the factory model encapsulates the entire creation process of the object in the factory class, and the factory class provides the final product to the client; in the builder mode, the builder class generally only provides the construction of each component in the product class. The specific construction process is delivered to the director class. The director class is responsible for forming each component into a product according to specific rules, and then delivering the assembled product to the client.

to sum up

The builder mode is similar to the factory mode, they are both builder mode, and the applicable scenarios are also very similar. Generally speaking, if the construction of the product is complex, then please use the factory model; if the construction of the product is more complex, then please use the builder model.

Guess you like

Origin blog.csdn.net/GTC_GZ/article/details/108754475