原创 Python建造者模式

建造者模式,實例的參數初始化由建造類方法完成。 class Instance(object): def __init__(self, builder): super().__init__()

原创 Python單例模式

單例模式,多次實例化只返回同一個類實例。 class SimpleOne(object): __one = None def __init__(self): super().__init__()

原创 Python抽象工廠模式

抽象工廠模式,一個抽象工廠可以生產同一系列的多個型號產品實例。 class TypeB(object): def __init__(self): pass class TypeA(object):

原创 Python原型模式

原型模式,實例提供clone方法,獲取與當前相同的實例,並允許設置新的參數。 class ProtoType(object): def __init__(self): super().__init__()

原创 Python外觀模式

外觀模式,對一些類進行組合並可能添加新的簡易方法,效果類似一個導航條。 class ModuleA(object): def __init__(self): pass def do_work(self):

原创 Python訪問者模式

訪問者模式,一個物體針對不同的訪問者,所展現的行爲是不同的。 class Visitor(object): def __init__(self, type_): self.type_ = type_ class

原创 Python策略模式

策略模式,同一問題有多種不同的解法,即不同策略,一個物體可以動態地對策略進行更換。 class Stragety(object): def __init__(self, name): self.name = nam

原创 Python狀態模式

狀態模式,就像某一個人在兒童、青年、老年所展現的狀態是不同的。 class Obj(object): def __init__(self, status): self.status = status de

原创 Python中介者模式

中介者模式,中介者提供多人聯繫溝通的平臺。 class Mediator(object): def __init__(self): self.online_dct = {} def register(se

原创 Python設計模式目錄

設計模式分爲3個模塊,創建類型、結構類型和行爲類型。創建類型關注於實例的創建,結構類型關注於不同類的之間的關係,行爲類型關注於類之間的通信。 目錄 創建類 1. 單例模式 2. 工廠模式 3. 抽象工廠模式 4. 建造者模式 5

原创 Python模板方法模式

模板方法模式,在父類中確定步驟的執行過程,子類無法更改執行過程順序。 class Template(object): def __init__(self): super().__init__() def

原创 Python觀察者模式

觀察者模式,被觀察物自身屬性通知觀察者。 class Obj(object): def __init__(self, value): super().__init__() self.observer

原创 Python橋接模式

橋接模式,一個類包含不同種類屬性,將不同種類的屬性分別轉爲不同的類。 class Type(object): def __init__(self, type_): self.type = type_ de

原创 Python備忘錄模式

備忘錄模式,類似於撤銷功能,提供保存並恢復之前狀態的功能。 class Momento(object): def __init__(self): super().__init__() self.dc

原创 Python組合模式

組合模式,像樹的枝和葉一樣進行組合。 class Branch(object): def __init__(self): super().__init__() self.branch = []