5 - Design Patterns - Builder Pattern

Builder Pattern Concept

The following is excerpted from the rookie tutorial

Intent: To separate a complex build from its representation so that the same build process can create different representations.

Main solution: The main solution is to solve the problem of creating a "complex object" in a software system, which is usually composed of sub-objects of various parts with a certain algorithm; due to changes in requirements, various parts of this complex object often face drastic changes, but the algorithms that combine them are relatively stable.

When to use: When some basic components do not change, but their combinations change frequently.

How to fix it: Separate change from change.

Key code: Builder: Create and provide instances, Director: Manage dependencies of built instances.

Application examples:
1. When going to KFC, hamburgers, cola, french fries, fried chicken wings, etc. are unchanged, and their combinations are often changed, resulting in a so-called "package".
2. StringBuilder in JAVA.

Advantages:
1. The builder is independent and easy to expand.
2. It is easy to control the risk of details.

Disadvantages:
1. The products must have something in common, and the scope is limited.
2. If the internal changes are complicated, there will be many construction types.

Usage scenarios:
1. The objects to be generated have complex internal structures.
2. The internal properties of the objects that need to be generated are mutually dependent on each other.

Note: The difference from the factory mode is that the builder mode is more concerned with the order of assembly of parts.

accomplish

It is known that real estate developers build buildings, and the houses have room and hall differences, such as one room and one hall, two rooms and two halls, three rooms and three halls, 2 rooms and 0 halls, etc.

Product:

Represents a constructed complex object. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including defining the classes that make up the components, including the interfaces for assembling these components into the final product.
Here, represents the house, Room N N Hall.

public class House {
    private int room;
    private int livingRoom;
    public int getRoom() {
        return room;
    }
    public void setRoom(int room) {
        this.room = room;
    }
    public int getLivingRoom() {
        return livingRoom;
    }
    public void setLivingRoom(int livingRoom) {
        this.livingRoom = livingRoom;
    }

}

Builder:

Specifies abstract interfaces for the various components that create a product object.
Here, builder represents the process of building a house, and ultimately building a successful return to the built house

public interface HouseBuilder {
    /**
     * 建造卧室
     * @return void
     * 时间:2018年4月24日
     */
    public void buildRoom();
    /**
     * 建造客厅
     * @return void
     * 时间:2018年4月24日
     */
    public void buileLivingRoom();

    /**
     * 获取具体的房屋
     * @return House
     * @return
     * 时间:2018年4月24日
     */
    House getHouse();
}

Concrete Builder:

Implement the Builder's interface to construct and assemble the various parts of the product, define and specify the representation it creates, and provide an interface for retrieving the product.
We all know that there are many kinds of houses, which can be one bedroom and one living room, two bedrooms and one living room, three bedrooms and two living rooms and so on. One of them is represented here.

public class ConcreteHouseBuilder implements HouseBuilder {

    House house = new House();

    @Override
    public void buildRoom() {
        house.setRoom(4);
        System.out.println("four room");
    }

    @Override
    public void buileLivingRoom() {
        house.setLivingRoom(2);
        System.out.println("two living room");
    }

    @Override
    public House getHouse() {
        return house;
    }

}

Director:

Constructs an object using the Builder interface to guide the build process.
Here, it can be understood as a house salesperson, who is responsible for receiving customers and selecting the N rooms and N halls required by customers.

public class Director {
    /**
     * Director指挥builder建造要求的房屋
     * @return void
     * @param builder
     * 时间:2018年4月24日
     */
    public void coustruct(HouseBuilder builder) {
        builder.buildRoom();
        builder.buileLivingRoom();
    }
}

customer class

Buyers

public class Client {
    public static void main(String[] args) {
        Director director = new Director();
        HouseBuilder builder = new ConcreteHouseBuilder();
        director.coustruct(builder);
    }
}

Summarize

In fact, for the builder pattern, a particularly prominent point is: the separation of construction and presentation, so that the same construction process can create different presentations.
As can be seen in the above example

  • The representation of the house has many existences such as one bedroom and one hall, two bedrooms and one hall, three bedrooms and two halls, etc.
  • But the building process of the house is the same, there are both rooms and halls
  • We can separate the construction process of its rooms and halls from the representation of several rooms and several halls
  • So that as long as we focus on the construction of rooms and halls, the specific rooms and halls are also realized by specific builder classes.
  • Then the Director is responsible for communicating with the Client
  • At the same time, if we need a new room, we only need to create a specific builder class to implement the Builder interface, which satisfies the open-closed principle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851722&siteId=291194637
Recommended