日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python设计模式之外观模式实例讲解

發布時間:2024/5/7 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python设计模式之外观模式实例讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python中設計模式之外觀模式主張以分多模塊進行代碼管理而減少耦合,下面用實例來進行說明。

應用特性:

在很多復雜而小功能需要調用需求時,而且這些調用往往還有一定相關性,即一調用就是一系列的。

結構特性:

把原本復雜而繁多的調用,規劃統一到一個入口類中,從此只通過這一個入口調用就可以了。

代碼結構示例:

class ModuleOne(object):def Create(self):print 'create module one instance'def Delete(self):print 'delete module one instance' class ModuleTwo(object):def Create(self):print 'create module two instance'def Delete(self):print 'delete module two instance' class Facade(object):def __init__(self):self.module_one = ModuleOne()self.module_two = ModuleTwo()def create_module_one(self):self.module_one.Create()def create_module_two(self):self.module_two.Create()def create_both(self):self.module_one.Create()self.module_two.Create()def delete_module_one(self):self.module_one.Delete()def delete_module_two(self):self.module_two.Delete()def delete_both(self):self.module_one.Delete()self.module_two.Delete()

有點類似代理模式,不同之處是,外觀模式不僅代理了一個子系統的各個模塊的功能,同時站在子系統的角度,通過組合子系統各模塊的功能,對外提供更加高層的接口,從而在語義上更加滿足子系統層面的需求。

隨著系統功能的不斷擴張,當需要將系統劃分成多個子系統或子模塊,以減少耦合、降低系統代碼復雜度、提高可維護性時,代理模式通常會有用武之地。

再來看一個例子:

class small_or_piece1: def __init__(self): pass def do_small1(self): print 'do small 1' class small_or_piece_2: def __init__(self): pass def do_small2(self): print 'do small 2' class small_or_piece_3: def __init__(self): pass def do_small3(self): print 'do small 3' class outside: def __init__(self): self.__small1 = small_or_piece1() self.__small2 = small_or_piece_2() self.__small3 = small_or_piece_3() def method1(self): self.__small1.do_small1() ##如果這里調用的不只2兩函數,作用就顯示出來了,可以把原本復雜的函數調用關系清楚化,統一化 self.__small2.do_small2() def method2(self): self.__small2.do_small2() self.__small3.do_small3() if __name__ == '__main__': osd = outside() osd.method1() osd.method2()

結果:

do small 1 do small 2 do small 2 do small 3

總結

以上是生活随笔為你收集整理的Python设计模式之外观模式实例讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。