Builder model

The Builder pattern is used to create complex objects. Using the Builder pattern enables complex processes to be hierarchical and clear, and decouples the creation and use of objects. In fact, from the point of view of the code, it is repeated encapsulation, so that the code structure is more standard and reasonable, the hierarchical structure is more distinct.

In a complex object, a lot of code needs to be written to implement this class, and there may be a lot of parameters to initialize.

The Builder pattern breaks down the implementation of a class into multiple creation steps, which are typically represented in anthropomorphic form in most relevant sources. Such as builder, director, and director, but the personification of these terms does not fully enable the reader to get the full meaning. The following details what these roles need to do.

This article is a hydrology, using building a house as a simple example to explain.

First we create a base class for the house to simulate the house:

class House:# House base class
    def __init__(self):
        self.size=0
        self.floor=0
        self.garage=0
        self.courtyard=0
    def __str__(self):
        houseInfo='My house has'+str(self.size)+'Square meters,'+str(self.floor)+', '+str(self.garage)+'Garage'+str(self.courtyard)+'A yard'
        return houseInfo
Copy the code

Size, floor, garage, courtyard are used to record the information of the house, so that workers can record it when building the house later.

Next we create a worker class to specifically build our house:

class HouseBuilder:# Builder worker
    def __init__(self):
        self.house=House()
    def buildGarage(self,amount):
        self.house.garage=amount
        print(self.house)
    def buildFloor(self,amount):
        self.house.floor=amount
        print(self.house)
    def buildSize(self,amount):
        self.house.size=amount
        print(self.house)
    def buildCourtyard(self,amount):
        self.house.courtyard=amount
        print(self.house)
Copy the code

The building size footprint, number of garage garages, number of floors, and number of courtyard yards are defined, and a parameter is passed in each method to set this information.

After the workers, and then a contractor, contractor better to overall planning for the construction of appropriate, but also with the user docking person.

class Foreman:# the captain
    def __init__(self):
        self.builder=HouseBuilder()
    def build(self,size,floor,garage,courtyard):# Build steps
        self.builder.buildSize(size)
        self.builder.buildFloor(floor)
        self.builder.buildGarage(garage)
        self.builder.buildCourtyard(courtyard)
Copy the code

In the contractor’s initialization function, create a new worker class, that is, the builder of the house, and then define a build method for the construction. During the construction, it usually determines the area first, then builds the house (floor), and then builds the garage and yard.

This time a simple model was finished, the builder in order to better, I built a User class, the User class don’t need to know which specific completed, only need docking contractor, contractor told me this time covers the size, the size and some parameters, contractor told to his younger brother, At this point, construction can begin and the user does not need to know about the construction process. The code is as follows:

class User(a):# the user
    def __init__(self,size,floor,garage,courtyard):
        self.frm=Foreman()
        self.frm.build(size,floor,garage,courtyard)
Copy the code

The complete code is as follows:

class House:# House base class
    def __init__(self):
        self.size=0
        self.floor=0
        self.garage=0
        self.courtyard=0
    def __str__(self):
        houseInfo='My house has'+str(self.size)+'Square meters,'+str(self.floor)+', '+str(self.garage)+'Garage'+str(self.courtyard)+'A yard'
        return houseInfo
        
class HouseBuilder:# Builder worker
    def __init__(self):
        self.house=House()
    def buildGarage(self,amount):
        self.house.garage=amount
        print(self.house)
    def buildFloor(self,amount):
        self.house.floor=amount
        print(self.house)
    def buildSize(self,amount):
        self.house.size=amount
        print(self.house)
    def buildCourtyard(self,amount):
        self.house.courtyard=amount
        print(self.house)

class Foreman:# the captain
    def __init__(self):
        self.builder=HouseBuilder()
    def build(self,size,floor,garage,courtyard):# Build steps
        self.builder.buildSize(size)
        self.builder.buildFloor(floor)
        self.builder.buildGarage(garage)
        self.builder.buildCourtyard(courtyard)

class User(a):# the user
    def __init__(self,size,floor,garage,courtyard):
        self.frm=Foreman()
        self.frm.build(size,floor,garage,courtyard)

        
xiaoming=User(500.3.2.1)
Copy the code

The running results are as follows: