Builder模式

Builder模式將一個複雜對象的構建與他的表示分離,使得一樣的構建過程能夠建立不一樣的表示。python

#Director
class Director(object):
    def __init__(self):
        self.bulider = None     
    @property
    def Director_building(self):
        self.builder.new_building()
        self.builder.build_floor()
        self.builder.build_size()
    @Director_building.setter
    def Director_building(self, BuilderSite=None):
        self.builder = BuilderSite
    
    def get_building(self):
        return self.builder.building
    
    
#Abstract Builder
class Builder(object):
    def __init__(self):
        self.building = None     
    def new_building(self):
        self.building = Building() 
#Concrete Builder
class BuilderHouse(Builder):
    def build_floor(self):
        self.building.floor = 'One'     
    def build_size(self):
        self.building.size = 'Big' 
class BuliderFlat(Builder):
    def build_floor(self):
        self.building.floor = 'More than One'     
    def build_size(self):
        self.building.size = 'Small' 
#Product
class Building(object):
    def __init(self):
        self.floor = None
        self.size = None     
    def __repr__(self):
        return 'Floor: %s | Size: %s' % (self.floor, self.size) #Client
if __name__ == "__main__":
    director = Director()
    director.Director_building=BuilderHouse()
    director.Director_building
    building = director.get_building()
    print(building)
    director.Director_building=BuliderFlat()
    director.Director_building
    building = director.get_building()
相關文章
相關標籤/搜索