当前位置: 首页 > 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()
http://www.lqws.cn/news/469747.html

相关文章:

  • 跳跳杆Pogo Stick
  • Swift 解锁数组可修改场景:LeetCode 307 高效解法全解析
  • (LeetCode 每日一题) 3085. 成为 K 特殊字符串需要删除的最少字符数 (贪心、哈希表)
  • 从0开始学习计算机视觉--Day02--数据驱动
  • MySQL之InnoDB存储引擎深度解析
  • Rust自动化测试的框架
  • Linux 系统结构划分详解:用户区与内核区的设计逻辑
  • 软件工程概述知识点总结
  • 1.23Node.js 中操作 mongodb
  • 基于机器学习的侧信道分析(MLSCA)Python实现(带测试)
  • 智慧医院核心引擎:IBMS 系统守护医疗环境高效与安全​
  • 浅议 3D 展示技术为线上车展新体验带来的助力​
  • Taro 跨端开发:从调试到发布的完整指南
  • CTF--PhP Web解题(走入CTF)
  • 贪心算法思路详解
  • Redis后端的简单了解与使用(项目搭建前置)
  • ARCGIS国土超级工具集1.6更新说明
  • 零基础学习Redis(12) -- Java连接redis服务器
  • 60-Oracle 10046事件-实操
  • Qt的学习(七)
  • 【价值链】产品经理
  • 学习C++、QT---03(C++的输入输出、C++的基本数据类型介绍)
  • nn4dms开源程序是用于深度突变扫描数据的神经网络
  • 车载电子电器架构 --- 法律和标准对电子电气架构的影响
  • JAVA锁机制:对象锁与类锁
  • Vue 简写形式全解析:清晰记忆指南
  • 自动化立体仓库堆垛机控制系统STEP7 FC3功能块 I/O映射
  • 【基础算法】二分(二分查找 + 二分答案)
  • Gunicorn 在 Windows 上能安装但无法运行的解决方案
  • 跟着AI学习C# Day29