Dahua design mode reading notes (builder mode)

People: small dish, big bird

Event: the small dish and the big bird went to have a snack. The small dish ate a bowl of delicious fried noodles, and then the big bird wanted to eat and ordered a bowl, but found that this bowl did not put salt. Why did the same cook cook cook fried noodles twice, the first bowl was delicious, but the second bowl was not delicious? Why did KFC McDonald's, although distributed all over the country, taste almost the same? The big bird led to the builder mode in the design mode

 

Builder pattern

1. Structural diagram of builder mode:

2. What is the builder model?

Answer:

Take a modeling example. When you need to build a fat person and a thin person, if you build a person's head, hands, stomach, feet, etc. in turn, you may lose arms and legs due to carelessness. Moreover, the needs of the builder may be different every time. Some require a thinner person and some require a longer hand. If you knock the code according to each part, there is a risk of error every time, Therefore, we should abstract the process of food processing and the process of building a person like McDonalds, so the part of building a person is a complete process, and there will be no lack of arms and legs.

In other words, the process of building a person is stable: head, hands and feet, and the details of building a person are different: tall, short, fat and thin. This separates the construction of a complex object from its representation, so that the same construction process can create different representation intentions, which is also known as the builder pattern.     

 

3. Basic code of the builder

Product category -- Product Category

@Slf4j
public class Product {
    List<String> parts = new ArrayList();

    public void add(String part) {
        parts.add(part);
    }

    public void show(){
        log.info("Products, creating: ");
        for(String s: parts) {
            log.info(s);
        }
    }
}

Builder class -- Abstract builder class: determine that the product is composed of two parts A and B, and finally use getResult() method to get the product

public abstract class Builder {
    public abstract void buildPartA();
    public abstract void buildPratB();
    public abstract Product getResult();
}

ConcreteBuilder1 and ConcreteBuilder2 are concrete builder classes

public class ConcreteBuilder1 extends Builder {
    private Product product = new Product();

    @Override
    public void buildPartA() {
        product.add("parts A");
    }

    @Override
    public void buildPratB() {
        product.add("parts B");
    }

    @Override
    public Product getResult() {
        return product;
    }
}
public class ConcreteBuilder2 extends Builder {
    private Product product = new Product();

    @Override
    public void buildPartA() {
        product.add("parts X");
    }

    @Override
    public void buildPratB() {
        product.add("parts Y");
    }

    @Override
    public Product getResult() {
        return product;
    }
}

Director class -- director class

public class Director {
    public void construct(Builder builder) {
        builder.buildPartA();
        builder.buildPratB();
    }
}

Client code call:

public static void main(String[] args) {
    Director director = new Director();
    Builder b1 = new ConcreteBuilder1();
    Builder b2 = new ConcreteBuilder2();

    director.construct(b1);
    Product p1 = b1.getResult();
    p1.show();

    director.construct(b2);
    Product p2 = b2.getResult();
    p2.show();
}

Summary: therefore, the builder pattern is the pattern applicable when the algorithm for creating complex objects should be independent of the components of the objects and their assembly methods

Tags: Design Pattern

Posted by scm24 on Wed, 25 May 2022 18:14:28 +0300