Builder mode - and easy to understand!!!

Builder pattern

Building project demand

Building project demand

  1. Need to build houses: this process involves piling, walling and capping
  2. There are various kinds of houses, such as ordinary houses, tall buildings, villas, although the process of various houses is the same, but
    Don't ask for the same
  3. Please write the program to complete the requirement
Code implementation:
House class: no matter what kind of house he has floor, wall and roof
package Design pattern.Builder pattern .First;

/**
 * @author Sun Yiming on February 7, 2020
 */
public class House {
    //floor
    private String floor;

    //wall
    private String wall;

    //Roof
    private  String housetop;

    public String getFloor() {
        return floor;
    }

    public void setFloor(String floor) {
        this.floor = floor;
    }

    public String getWall() {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getHousetop() {
        return housetop;
    }

    public void setHousetop(String housetop) {
        this.housetop = housetop;
    }
}

[scheme 1:] the house, floor, wall and roof are all made by customers themselves

public class Client {

    public static void main(String[] args) {
        //Customers build houses directly
        House house = new House();
        house.setFloor("floor");
        house.setWall("wall");
        house.setHousetop("Roof");
    }
}

Obviously, this way is not suitable. At this time, the house construction team will come out. The so-called professional construction team has no corresponding materials for building high-rise buildings. But when they build buildings, they need to lay floors, build walls, build roofs. All of them have to work. Therefore, in order to cope with various house manufacturing situations (high-rise buildings, villas, bungalows, etc.) ), we set up a construction team template (Interface), let the construction team with different materials to achieve it

public interface HouseBuilder {
    //Floor repair
    public  void makeFloor();
    //Mending walls
    public  void makeWall();
    //Roofing
    public  void makeHousetop();
}

Entrance of bungalow construction team:

/**
 * @author Sun Yiming on February 7, 2020
 */
public class PingFangBuilder implements HouseBuilder {
    //The bungalow construction team must first have the foundation of the house, so as to continuously construct on the foundation
    House house = new House();

    @Override
    public void makeFloor() {
        house.setFloor("to--Bungalow--Flooring");
    }

    @Override
    public void makeWall() {
        house.setWall("to--Bungalow--Wall");
    }

    @Override
    public void makeHousetop() {
        house.setHousetop("to--Bungalow--Build roofs");
    }
    //After the construction team completes, hand over the house
    public House getHouse(){
        return house;
    }
}

[scheme 2:] the client has contacted a bungalow construction team, which is built by the construction team and the client receives the house

package Design pattern.Builder pattern .First;

/**
 * @author Sun Yiming on February 7, 2020
 */
public class Client {

    public static void main(String[] args) {
        //Customers build houses directly
//        House house = new House();
//        house.setFloor("floor");
//        house.setWall("wall");
//        house.setHousetop("roof");

        //A construction team built the house
        //1. The customer contacted a bungalow construction team
        PingFangBuilder builder = new PingFangBuilder();
        builder.makeFloor();
        builder.makeWall();
        builder.makeHousetop();
        House house = builder.getHouse();
        System.out.println("house = " + house);
    }
}


At this time, it's time to introduce our leading role today --- builder mode. Let's take a look at this picture first

We found that the above customers also need to contact the construction team by themselves. It is not a good plan for the customers to directly communicate with the construction team. We need to communicate with the management personnel of the construction team, and have their own management personnel to communicate with their own construction team. Therefore, we introduced a new member Director, known as the contractor leader
Implementation of contractor's Director Code:

package Design pattern.Builder pattern .First;

/**
 * @author Sun Yiming on February 7, 2020
 */
public class A contractor {
    //The head contractor has his own construction team
  //  private HouseBuilder builder;

   // public contractor (HouseBuilder builder){
   //     this.builder = builder;
  //  }
    //The head contractor commands his own construction team
    public void makeHouse(HouseBuilder builder){
        builder.makeFloor();
        builder.makeWall();
        builder.makeHousetop();
    }
}

[scheme 3:] the client contacts the contractor to ask the contractor to direct the construction team to build the house and the client to close the house

package Design pattern.Builder pattern .First;

/**
 * @author Sun Yiming on February 7, 2020
 */
public class Client {

    public static void main(String[] args) {
        //Customers build houses directly
//        House house = new House();
//        house.setFloor("floor");
//        house.setWall("wall");
//        house.setHousetop("roof");

        //A construction team built the house
        //1. The customer contacted a bungalow construction team
//        PingFangBuilder builder = new PingFangBuilder();
//        builder.makeFloor();
//        builder.makeWall();
//        builder.makeHousetop();
//        House house = builder.getHouse();
//        System.out.println("house = " + house);

        //The client contacts the contractor and asks the contractor to direct the construction team to build the house
        //Construction team
        PingFangBuilder builder = new PingFangBuilder();
        //Contractor director =new A contractor();
        //The head Contractor shall direct the construction team to work...
        director.makeHouse(builder);
        House house = builder.getHouse();
        System.out.println("house = " + house);
    }
}

At this time, the builder mode has been expressed. If we have money now and want to build high-rise buildings, how can we modify the code?
To build the building, we will contact the contractor leader to direct our high-rise construction team builder
High building construction team builder code realization:

package Design pattern.Builder pattern .First;

/**
 * @author Sun Yiming on February 7, 2020
 */
public class HighBuilder implements HouseBuilder {
    //The high-rise construction team must first have the foundation of the house, so as to continuously construct on the foundation
    House house = new House();

    @Override
    public void makeFloor() {
        house.setFloor("to--Tall building--Flooring");
    }

    @Override
    public void makeWall() {
        house.setWall("to--Tall building--Wall");
    }

    @Override
    public void makeHousetop() {
        house.setHousetop("to--Tall building--Build roofs");
    }
    //After the construction team completes, hand over the house
    public House getHouse(){
        return house;
    }
}
  //The client contacts the contractor and asks the contractor to direct the construction team to build the house
        //High rise construction team
        HighBuilder builder =new HighBuilder();
        //Contractor director =new A contractor();
        //The head Contractor shall direct the construction team to work...
        director.makeHouse(builder);
        House house = builder.getHouse();
        System.out.println("house = " + house);

We need to connect the theory to see the builder model

Basic introduction:
  1. Builder Pattern, also known as generator pattern, is an object building pattern. It can
    Abstract the construction process of complex objects (abstract categories), making different implementers of this abstract process
    Method can construct objects with different properties.

Complex objects: Houses
Build different houses through different construction teams

  1. Builder mode is to create a complex object step by step, which allows users to specify only complex objects
    The type and content of can build them, users do not need to know the specific internal construction details.

The customer only needs to contact the contractor to get the house

Four roles of builder model
  1. Product: a specific product object. = = = house
  2. Builder: creates an interface / abstract class specified by each part of a Product object.
  3. Concrete Builder: implement interfaces, build and assemble components. Specific construction team
  4. Director contracting head: build an object using the Builder interface. It is mainly used to create a complex object. It has two main functions: one is to isolate the production process of customers and objects; the other is to:
    Be responsible for controlling the production process of product objects.
Precautions and details of builder mode:
  1. The client (user program) does not need to know the details of the internal composition of the product, and understands the product itself and the creation process of the product
    Coupling enables the same creation process to create different product objects

  2. Each specific builder is relatively independent, and has nothing to do with other specific builders, so it is very convenient to replace
    Change specific builder or add new specific builder, users can get different with different specific builder
    Product object of

  3. **You can control the product creation process more precisely. **Decompose the creation steps of complex products in different ways
    It makes the creation process clearer and easier to use the program to control the creation process

  4. Adding a new concrete builder does not need to modify the code of the original class library. The director class is programmed for the abstract builder class,
    The system is easy to expand and conforms to the "opening and closing principle"

  5. Generally, the products created by the builder mode have more common points, and their components are similar if the products are different from each other
    Because of the great difference, it is not suitable to use builder mode, so its use scope is limited.

  6. If the internal changes of the product are complex, it may lead to the need to define many specific builder classes to implement such changes,
    As a result, the system becomes very large, so in this case, consider whether to choose builder mode

  7. Abstract factory mode VS builder mode
    Abstract factory pattern realizes the creation of product family. A product family is a series of products with no
    For the product portfolio with the same classification dimension, the abstract factory model does not need to care about the construction process, but only about what products
    What kind of factory can produce it. The builder mode is to build products according to the specified blueprint, and its main
    The aim is to produce a new product by assembling parts

55 original articles published, 20 praised, 10000 visitors+
Private letter follow

Posted on Fri, 07 Feb 2020 06:00:14 -0800 by wispas