python builder mode

python builder mode

class AppleFactory:
    class MacMini14:
        def __init__(self):
            self.memory = 4 # 单位为GB
            self.hdd = 500 # 单位为GB
            self.gpu = 'Intel HD Graphics 5000'
        def __str__(self):
            info = ('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 dont't know how to build {}".format(model))
if __name__ == '__main__':
    MINI14 = '1.4GHz Mac mini'
    afac = AppleFactory()

    mac_mini = afac.build_computer(MINI14)
    print(mac_mini)

If we know that an object must go through multiple steps to create, and require the same construction process can produce different
performance, we can use the builder mode. This requirement exists in many applications, such as page generators (HTML
page generators mentioned in this chapter ), document converters, and user interface (UI) form creation tools.
Some sources mention that the builder pattern can also be used to solve the problem of scalable constructors.
When we have to create a new constructor in order to support different object creation methods, the scalable constructor problem occurs
. This situation eventually produces many constructors and long parameter lists, which are difficult to manage.
An example of a scalable constructor is listed on the Stack Overflow website . Fortunately, this problem
does not exist in Python , because there are at least the following two ways to solve this problem.
 Use named parameters
 Use actual parameter list to expand
At this point, the difference between builder mode and factory mode is not clear. The main difference is that the factory mode
creates objects in a single step , while the builder mode creates objects in multiple steps, and almost always uses a leader. Some targeted
builder pattern implementations do not use commanders, such as Java's StringBuilder, but this is only an exception.
Another difference is that in the factory mode, a created object is returned immediately; in the builder mode, the
client code explicitly requests the commander to return the final object only when needed .
The example of the new computer analogy may help distinguish the builder model from the factory model. Suppose you want to buy a new computer, if you
decide to buy a specific pre-configured computer model, for example, the latest Apple 1.4GHz Mac mini, you are using the factory
mode. All hardware specifications have been predetermined by the manufacturer, and the manufacturer knows what to do without consulting you.
They usually receive only a single instruction.

class Computer:
    def __init__(self, serial_number):
        self.serial = serial_number
        self.memory = None # 单位为GB
        self.hdd = None # 单位为GB
        self.gpu = None
    def __str__(self):
        info = ('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))]
    @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()

Insert picture description here

Guess you like

Origin blog.csdn.net/qestion_yz_10086/article/details/108257975