当前位置: 首页 > news >正文

【设计模式】3.装饰模式

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

装饰模式

1. 第一版

class Person:def __init__(self, name):self.name = namedef wear_t_shirts(self):print("大T恤 ", end="")def wear_big_trouser(self):print("垮掉 ", end="")def wear_sneakers(self):print("破球鞋 ", end="")def wear_suit(self):print("西装 ", end="")def wear_tie(self):print("领带 ", end="")def wear_leather_shoes(self):print("皮鞋 ", end="")def show(self):print(f"装扮的{self.name}")# 客户端代码
if __name__ == "__main__":xc = Person("小菜")print("\n第一种装扮: ")xc.wear_t_shirts()xc.wear_big_trouser()xc.wear_sneakers()xc.show()print("\n第二种装扮: ")xc.wear_suit()xc.wear_tie()xc.wear_leather_shoes()xc.show()input()  # 相当于C#的Console.Read()

增加"超人"类,需要修改Person,违反了开放-封闭原则

2. 第二版

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

from abc import ABC, abstractmethod# Person 类
class Person:def __init__(self, name):self.name = namedef show(self):print(f"装扮的{self.name}")# 服饰抽象类
class Finery(ABC):@abstractmethoddef show(self):pass# 具体服饰类
class TShirts(Finery):def show(self):print("大T恤 ", end="")class BigTrouser(Finery):def show(self):print("垮裤 ", end="")class Sneakers(Finery):def show(self):print("破球鞋 ", end="")class Suit(Finery):def show(self):print("西装 ", end="")class Tie(Finery):def show(self):print("领带 ", end="")class LeatherShoes(Finery):def show(self):print("皮鞋 ", end="")# 客户端代码
if __name__ == "__main__":xc = Person("小菜")print("\n第一种装扮:")dtx = TShirts()kk = BigTrouser()pgx = Sneakers()dtx.show()kk.show()pgx.show()xc.show()print("\n第二种装扮:")xz = Suit()ld = Tie()px = LeatherShoes()xz.show()ld.show()px.show()xc.show()

建造者模式过程必须稳定,而这里建造过程不稳定,可以先传西装,外套,也可以反过来。

3. 第三版

装饰模式
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

from abc import ABC, abstractmethod# Person类 (ConcreteComponent)
class Person:def __init__(self, name):self.name = namedef show(self):print(f"装扮的{self.name}")# 服饰抽象类 (Decorator)
class Finery(Person):def __init__(self):self._component = None# 打扮/装饰方法def decorate(self, component):self._component = componentdef show(self):if self._component:self._component.show()# 具体服饰类 (ConcreteDecorator)
class TShirts(Finery):def show(self):print("大T恤 ", end="")super().show()class BigTrouser(Finery):def show(self):print("垮裤 ", end="")super().show()class Sneakers(Finery):def show(self):print("破球鞋 ", end="")super().show()class Suit(Finery):def show(self):print("西装 ", end="")super().show()class Tie(Finery):def show(self):print("领带 ", end="")super().show()class LeatherShoes(Finery):def show(self):print("皮鞋 ", end="")super().show()# 客户端代码
if __name__ == "__main__":xc = Person("小菜")print("\n第一种装扮:")pqx = Sneakers()kk = BigTrouser()dtx = TShirts()pqx.decorate(xc)    # 装饰过程kk.decorate(pqx)dtx.decorate(kk)dtx.show()print("\n第二种装扮:")px = LeatherShoes()ld = Tie()xz = Suit()px.decorate(xc)     # 装饰过程ld.decorate(px)xz.decorate(ld)xz.show()input()  # 相当于C#的Console.Read()

相关文章:

  • leetcode332.重新安排行程:优先队列与DFS实现欧拉路径的行程规划
  • AIGC工具平台-SadTalker音频对口型数字人
  • Linux 内核中 TCP 协议栈的输出实现:tcp_output.c 文件解析
  • 2D曲线点云平滑去噪
  • (LeetCode 面试经典 150 题) 169. 多数元素(哈希表 || 二分查找)
  • python基础(while...else)
  • 手撕lru
  • MinIO入门教程:从零开始搭建方便快捷的分布式对象存储服务
  • 蓝桥杯备赛篇(上) - 参加蓝桥杯所需要的基础能力 1(C++)
  • 【Linux第四章】gcc、makefile、git、GDB
  • 用OBS Studio录制WAV音频,玩转语音克隆和文本转语音!
  • MySQL之InnoDB存储引擎深度解析
  • PowerShell读取CSV并遍历组数组
  • 3.8 恢复行为
  • 微处理器原理与应用篇---冯诺依曼体系结构
  • 一文详解归并分治算法
  • Python元组常用操作方法
  • 在 ArcPy 脚本中进行错误处理和调试
  • C# WPF常用调试工具汇总
  • MagicTryOn: 变革性的AI视频虚拟试衣体验
  • 江苏营销型网站策划/北京seo工程师
  • 莘庄做网站/站长之家seo工具包
  • 做网站要学些什么软件/seo优化推广工程师
  • 网站开发与建设方向/手机app推广平台
  • 扬州网站优化/百度指数专业版价格
  • 学校网站首页模板/如何制作一个网页页面