建造模式

和第一個抽象工廠模式有點像,繼承共同的抽象類,統一出口,不過這裏的出口是生產對象進程,抽象工廠模式是統一方法ide


class Building(object):
    def __init__(self):
        self.floor = None
        self.size = Noneui

    def __repr__(self):
        return 'Floor: {0.floor} | Size: {0.size}'.format(self)
class Builder(object):orm

    def __init__(self):
        self.building = None對象

    def new_building(self):
        self.building = Building()繼承

    def build_floor(self):
        raise NotImplementedError進程

    def build_size(self):
        raise NotImplementedErrorit

class BuilderHouse(Builder):form

    def build_floor(self):
        self.building.floor = 'One'class

    def build_size(self):
        self.building.size = 'Big'object


class BuilderFlat(Builder):

    def build_floor(self):
        self.building.floor = 'More than One'

    def build_size(self):
        self.building.size = 'Small'

def construct_building(builder):
    bulider.new_building()
    builder.build_floor()
    builder.build_size()
    return builder.building

# Client if __name__ == "__main__":     building = construct_building(BuilderHouse())     print(building)     building = construct_building(BuilderFlat())     print(building)

相關文章
相關標籤/搜索