內容:將一個類的接口轉換成客戶但願的另外一個接口。適配器模式使得本來因爲接口不兼容而不能一塊兒工做的那些類能夠一塊兒工做。安全
兩種實現方式:微信
角色:app
一、目標接口(target)ide
二、待適配的類(Adaptee)微信支付
三、適配器(Adapter)優化
適用場景:spa
想使用一個已經存在的類,而它的接口不符合你的要求代理
(對象適配器)想使用一些已經存在的子類,但不可能對每個都進行子類化以匹配它們的接口。對象適配器能夠適配它的父類接口。code
from abc import ABCMeta, abstractmethod class Payment(metaclass=ABCMeta): # abstract class @abstractmethod def pay(self, money): pass class Alipay(Payment): def pay(self, money): print("支付寶支付%d元." % money) class WechatPay(Payment): def pay(self, money): print("微信支付%d元." % money) class BankPay: def cost(self, money): print("銀聯支付%d元." % money) class ApplePay: def cost(self, money): print("蘋果支付%d元." % money) # 類適配器 # class PaymentAdapter(Payment, BankPay): # def pay(self, money): # self.cost(money) # 對象適配器 class PaymentAdapter(Payment): def __init__(self, payment): self.payment = payment def pay(self, money): self.payment.cost(money) p = PaymentAdapter(BankPay()) p.pay(100) ## 目標接口:Payment 待適配的類:BankPay, ApplePay 適配器:PaymentAdapter
內容:將一個事物的兩個維度分離,使其均可以獨立地變化。對象
角色:
應用場景:當事物有兩個維度上的表現,兩個維度都有可能擴展時。
優勢:
from abc import ABCMeta, abstractmethod class Shape(metaclass=ABCMeta): def __init__(self, color): self.color = color @abstractmethod def draw(self): pass class Color(metaclass=ABCMeta): @abstractmethod def paint(self, shape): pass class Rectangle(Shape): name = "長方形" def draw(self): # 長方形邏輯 self.color.paint(self) class Circle(Shape): name = "圓形" def draw(self): # 圓形邏輯 self.color.paint(self) class Line(Shape): name = "直線" def draw(self): # 直線邏輯 self.color.paint(self) class Red(Color): def paint(self, shape): print("紅色的%s" % shape.name) class Green(Color): def paint(self, shape): print("綠色的%s" % shape.name) class Blue(Color): def paint(self, shape): print("藍色的%s" % shape.name) shape = Line(Blue()) shape.draw() shape2 = Circle(Green()) shape2.draw() ## 抽象:Shape 細化抽象:Color 實現者:Red,Green,Blue 具體實現者:Rectangle,Circle, Line
內容:將對象組合成樹型結構以表示"部分-總體"的層次結構。組合模式使得用戶對單個對象和組合對象的使用具備一致性。
角色:
適用場景:
表示對象的"部分-總體"層次結構(特別是結構是遞歸的)
但願用戶忽略組合對象與單個對象的不一樣,用戶統一地適用組合結構中的全部對象。
優勢:
from abc import ABCMeta, abstractmethod # 抽象組件 class Graphic(metaclass=ABCMeta): @abstractmethod def draw(self): pass # 葉子組件 class Point(Graphic): def __init__(self, x, y): self.x = x self.y = y def __str__(self): return "點(%s, %s)" % (self.x, self.y) def draw(self): print(str(self)) # 葉子組件 class Line(Graphic): def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def __str__(self): return "線段[%s, %s]" % (self.p1, self.p2) def draw(self): print(str(self)) # 複合組件 class Picture(Graphic): def __init__(self, iterable): self.children = [] for g in iterable: self.add(g) def add(self, graphic): self.children.append(graphic) def draw(self): print("------複合圖形------") for g in self.children: g.draw() print("------複合圖形------") p1 = Point(2,3) l1 = Line(Point(3,4), Point(6,7)) l2 = Line(Point(1,5), Point(2,8)) pic1 = Picture([p1, l1, l2]) p2 = Point(4,4) l3 = Line(Point(1,1), Point(0,0)) pic2 = Picture([p2, l3]) pic = Picture([pic1, pic2]) pic.draw()
內容:爲子系統中的一組接口提供一個一致的界面,外觀模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
角色:
優勢:
class CPU: def run(self): print("CPU開始運行") def stop(self): print("CPU中止運行") class Disk: def run(self): print("硬盤開始工做") def stop(self): print("硬盤中止工做") class Memory: def run(self): print("內存通電") def stop(self): print("內存斷電") class Computer: def __init__(self): self.cpu = CPU() self.disk = Disk() self.memory = Memory() def run(self): self.cpu.run() self.disk.run() self.memory.run() def stop(self): self.cpu.stop() self.disk.stop() self.memory.stop() computer = Computer() computer.run() computer.stop()
內容:爲其餘對象提供一種代理以控制對這個對象的訪問。
應用場景:
角色:
抽象實體(Subject)
實體(Realsubject)
代理(Proxy)
優勢:
from abc import ABCMeta, abstractmethod class Subject(metaclass=ABCMeta): @abstractmethod def get_content(self): pass @abstractmethod def set_content(self, content): pass class RealSubject(Subject): def __init__(self, filename): self.filename = filename f = open(filename, 'r', encoding="utf-8") print("讀取文件內容") self.content = f.read() f.close() def get_content(self): return self.content def set_content(self, content): f = open(self.filename, 'w', encoding="utf-8") f.write(content) f.close() class VirtualProxy(Subject): def __init__(self, filename): self.filename = filename self.subj = None def get_content(self): if not self.subj: self.subj = RealSubject(self.filename) return self.subj.get_content() def set_content(self, content): if not self.subj: self.subj = RealSubject(self.filename) return self.subj.set_content(content) class ProtectedProxy(Subject): def __init__(self, filename): self.subj = RealSubject(filename) def get_content(self): return self.subj.get_content() def set_content(self, content): raise PermissionError("無寫入權限") subj = ProtectedProxy("test.txt") print(subj.get_content()) subj.set_content("abc")