结构型
类与类之间 关系的一种模式
适配器模式
俩种方式
- 多继承
| |
| class BankPayment: |
| def cost(self, money): |
| print("银行支付") |
| |
| class NewBankPayment(Payment, BankPayment): |
| 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) |
桥模式
将一个事物的俩个维度分离,使其都可以独立地变化
形状和颜色
| # 颜色和形状紧密耦合 |
| class Shape: |
| pass |
| |
| class Line(Shape): |
| pass |
| |
| class Rectangle(Shape): |
| pass |
| |
| class Circle(Shape): |
| pass |
| |
| # 类爆炸 3 * 3 。 |
| class RedLine(Line): |
| # * 3 |
| pass |
| |
| class RedRectangle(Rectangle): |
| # * 3 |
| pass |
| |
改进后。
互相拥有
| 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 Line(Shape): |
| def draw(self): |
| self.color.paint(self) |
| |
| class Rectangle(Shape): |
| def draw(self): |
| self.color.paint(self) |
| |
| class Circle(Shape): |
| def draw(self): |
| self.color.paint(self) |
| |
| |
| class Red(Color): |
| def paint(self, shape): |
| print(" 红色的", shape) |
| |
| class Green(Color): |
| def paint(self, shape): |
| print(" 绿色的", shape) |
| |
| |
| shape1 = Line(Red()) |
| shape2 = Rectangle(Green()) |
| shape1.draw() |
| shape2.draw() |
组合模式
- 将对象组合成树形结构 以表示 部分-整体 的层次结构。单个对象和组合对象的使用具有一致性。
- 抽象组件、叶子组件、复合组件、客户端
- 部分和整体如何一致?使用统一的接口
| 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 draw(self): |
| print("Ponint:", self.x, self.y) |
| |
| class Line(Graphic): |
| def __init__(self, p1, p2): |
| self.p1 = p1 |
| self.p2 = p2 |
| |
| def draw(self): |
| print("Line:", self.p1, self.p2) |
| |
| class Picture(Graphic): |
| |
| def __init__(self, iterable): |
| self.children = [] |
| for g in iterable: |
| self.children.append(g) |
| |
| def draw(self): |
| for g in self.children: |
| g.draw() |
| |
| p1 = Point(1,2) |
| p2 = Point(3,4) |
| l1 = Line(p1, p2) |
| pic = Picture([p1, p2, l1]) |
| pic.draw() |
外观模式
- 为子系统中的一组接口提供一个一致的界面。外观模式定义了一个高层接口,这个接口使得这子系统更加容易使用
- 外观 facade
- 子系统类 subsystem classes
| |
| |
| class Compute: |
| '外观类' |
| def __init__(self): |
| self.disk = Disk() |
| self.cpu = CPU() |
| self.memory = Memory() |
| |
| def run(self): |
| self.disk.run() |
| self.cpu.run() |
| self.memory.run() |
| |
| def stop(self): |
| self.disk.stop() |
| self.cpu.stop() |
| self.memory.stop() |
| |
| class Disk: |
| |
| def run(self): |
| print("磁盘运行") |
| |
| def stop(self): |
| print("磁盘停止") |
| |
| class CPU: |
| def run(self): |
| print("CPU运行") |
| |
| def stop(self): |
| print("CPU停止") |
| |
| class Memory: |
| def run(self): |
| print("Memory运行") |
| |
| def stop(self): |
| print("Memory停止") |
| |
| c = Compute() |
| c.run() |
| c.stop() |
代理模式
- 为其他对象提供一种代理以控制对这个对象的访问。
- 远程代理、虚代理、保护代理
| from abc import ABCMeta,abstractmethod |
| |
| class Subject(metaclass=ABCMeta): |
| |
| @abstractmethod |
| def get_content(self): |
| pass |
| |
| @abstractmethod |
| def set_content(self): |
| pass |
| |
| class RealSubject(Subject): |
| def __init__(self, filename): |
| self.filename = filename |
| f = open(self.filename, 'r', encoding='utf-8') |
| 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 VirtualSubject(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 ProtectedSubject(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 = VirtualSubject('tree.py') |
| psubj = ProtectedSubject("tree.py") |
| psubj.set_content("dsafdsaf") |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统