Create a schema builder mode ---

Builder mode

We want to create an object composed of multiple parts, but its composition needs to be done step by step. Only when the various parts are created, the object is considered complete. That's where the builder design pattern. 
Model builder will construct a complex process of separation of the object and its performance, so that the same construction process can be used to create different performance. Examples of real life: fast-food restaurants use is the builder mode. Even the presence of a plurality of different packaging and hamburger, hamburger and a packaged preparation process is the same. The difference between the classic hamburger and cheeseburger that performance, rather than the construction process.
Commander cashier, what food products will need to be prepared to convey instructions to staff, were built by individual staff, concerns specific order.
Applications: The main difference between the builder pattern and factory pattern is that the factory pattern to create objects in a single step, and the builder pattern to create objects in multiple steps, and almost always use a commander. 
Another difference is that in the factory mode, it will immediately return a good object is created; and in the builder pattern, only when necessary client code was explicitly request the commander returns the final object.
# Suppose you want to buy a new computer, if you decide to also purchase a set of pre-configured computer models, such as Apple 1.4GHz Mac mini, it is the use of the factory model. 
# All specifications are already pre-determined by the manufacturer, what manufacturers do not have to consult you know what to do, they typically receive only a single instruction. 

# Factory pattern 
MINI14 = ' 1.4GHz Mac Mini ' 
class AppleFactory:
     class MacMini14:
         DEF  __init__ (Self): 
            self.memory = 4 
            self.hdd = 500 
            self.gpu = ' Intel HD Graphics 5000 ' 

        DEF  __str__ (Self): 
            info = ( " the Model: {} " .format (MINI14),
                    "Memory:{}GB".format(self.memory),
                    "Hard Disk:{}GB".format(self.hdd),
                    "Graphics Card:{}".format(self.gpu))
            return '\n'.join(info)

    def build_computer(self,model):
        if model==MINI14:
            return self.MacMini14()
        else:
            print("I don't know how to build {}.".format(model))

# if __name__ == '__main__':
#     = AppleFactory AFAC () 
#      mac_mini = afac.build_computer (MINI14) 
#      Print (mac_mini) 

# creator mode, customizable computer purchase mention Article, if such use is the builder pattern. You are commanded to provide Instructions hearts of the ideal computer specs from the manufacturer. 
class Computer:
     DEF  the __init__ (Self, serial_number): 
        self.serial = serial_number 
        self.memory = None 
        self.hdd = None 
        self.gpu = None 

    DEF  __str__ (Self): 
        info = ( " the Model: {} " .format (MINI14 ),
                 " Memory: GB {}".format(self.memory),
                "Hard Disk:{}GB".format(self.hdd),
                "Graphics Card:{}".format(self.gpu))
        return '\n'.join(info)

class ComputerBuilder:
    def __init__(self):
        self.computer = Computer('AG23385193')

    def configure_memory(self,amount):
        self.computer.memory = amount

    def configure_hdd(self,amount):
        self.computer.hdd = amount

    def configure_gpu(self,gpu_model):
        self.computer.gpu = gpu_model

class HardwareEngineer:
    def __init__(self):
        self.builder = None

    def construct_computer(self,memory,hdd,gpu):
        self.builder = ComputerBuilder()
        # [step for step in (self.builder.configure_memory(memory),
        #                    self.builder.configure_hdd(hdd),
        #                    self.builder.configure_gpu(gpu))]
        self.builder.configure_memory(memory)
        self.builder.configure_hdd(hdd)
        self.builder.configure_gpu(gpu)

    @property
    def computer(self):
        return self.builder.computer

def main():
    engineer = HardwareEngineer()
    engineer.construct_computer(hdd=500,memory=8,gpu='GeForce GTX 650 Ti')
    computer = engineer.computer
    print(computer)

if __name__ == '__main__':
    main()

summary

In the following cases, compared with the factory model, the builder pattern is a better choice.
    1 . I want to create a complex object (object is composed of a plurality of portions, and creating a plurality of objects to go through the different steps, these steps may need to follow particular order);
     2 . Requires an object can have different performance, and want to construct objects with the performance of decoupling;
     3. want to create an object at some point, but again access at a later point in time.

 

Guess you like

Origin www.cnblogs.com/xiaoshayu520ly/p/10981399.html