【编程思想】【设计模式】【创建模式creational】建造者模式builder
Python版
https://github.com/faif/python-patterns/blob/master/creational/builder.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/python # -*- coding : utf-8 -*- """ *What is this pattern about? It decouples the creation of a complex object and its representation, so that the same process can be reused to build objects from the same family. >>他降低了创建复杂类的难度和重现他的难度,因此相同的流程在相同家族总可以被重用 This is useful when you must separate the specification of an object from its actual representation (generally for abstraction). >>当你必须把它特殊的部分从他实际的表现中分离出来的时候很有用,通常用来做抽象 *What does this example do? The first example achieves this by using an abstract base class for a building, where the initializer (__init__ method) specifies the steps needed, and the concrete subclasses implement these steps. >>第一个例子达到了这个目的,他使用一个抽象的积累构建了一个building, >>初始化方法(__init__)指定了需要的方法,这个实际的子类实现了这些步骤 In other programming languages, a more complex arrangement is sometimes necessary. In particular, you cannot have polymorphic behaviour in a constructor in C++ - see https://stackoverflow.com/questions/1453131/how-can-i-get-polymorphic-behavior-in-a-c-constructor >>在其他的开发语言中,往往需要更加复杂的设计。就像在C++中不能使用多态一样 - which means this Python technique will not work. The polymorphism required has to be provided by an external, already constructed instance of a different class. >>这就意味着python技术不好使了。多态需要外部的,已经构成的,不同的类型的实例来支持 In general, in Python this won't be necessary, but a second example showing this kind of arrangement is also included. >>总的来说,在python中,这个不是必须的,但是第二个例子表明这种设计也是可以的 *Where is the pattern used practically? *References: https://sourcemaking.com/design_patterns/builder *TL;DR80 Decouples the creation of a complex object and its representation. """ # Abstract Building class Building(object): def __init__(self): self.build_floor() self.build_size() def build_floor(self): raise NotImplementedError def build_size(self): raise NotImplementedError def __repr__(self): return 'Floor: {0.floor} | Size: {0.size}'.format(self) # Concrete Buildings class House(Building): def build_floor(self): self.floor = 'One' def build_size(self): self.size = 'Big' class Flat(Building): def build_floor(self): self.floor = 'More than One' def build_size(self): self.size = 'Small' # In some very complex cases, it might be desirable to pull out the building # logic into another function (or a method on another class), rather than being # in the base class '__init__'. (This leaves you in the strange situation where # a concrete class does not have a useful constructor) class ComplexBuilding(object): def __repr__(self): return 'Floor: {0.floor} | Size: {0.size}'.format(self) class ComplexHouse(ComplexBuilding): def build_floor(self): self.floor = 'One' def build_size(self): self.size = 'Big and fancy' def construct_building(cls): building = cls() building.build_floor() building.build_size() return building # Client if __name__ == "__main__": house = House() print(house) flat = Flat() print(flat) # Using an external constructor function: complex_house = construct_building(ComplexHouse) print(complex_house) ### OUTPUT ### # Floor: One | Size: Big # Floor: More than One | Size: Small # Floor: One | Size: Big and fancy