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

python: Command Pattern

 

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:09
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : JewelCraftsman.py
# explain   : 学习from abc import ABC, abstractmethod# -------------------------- 1. 接收者(Receiver):珠宝工匠 --------------------------
class JewelCraftsman(object):"""珠宝工匠(真正执行具体操作的接收者)"""def polish_ring(self, ring_type: str):"""抛光戒指Args:ring_type:Returns:"""print(f"工匠正在抛光 {ring_type} 戒指,使其恢复光泽✨")def inlay_diamond(self, necklace: str, diamond_size: str):"""给项链镶嵌钻石Args:necklace:diamond_size:Returns:"""print(f"工匠正在给 {necklace} 项链镶嵌 {diamond_size} 钻石💎")def clean_necklace(self, necklace: str):"""清洗项链Args:necklace:Returns:"""print(f"工匠正在超声波清洗 {necklace} 项链,去除污渍🧼")# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:11
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : JewelCommand.py
# explain   : 学习from abc import ABC, abstractmethod# -------------------------- 2. 命令抽象类(Command) --------------------------
class JewelCommand(ABC):"""珠宝操作命令的抽象基类"""@abstractmethoddef execute(self):"""执行命令Returns:"""pass@abstractmethoddef undo(self):"""撤销命令(可选,增强功能)Returns:"""pass

  

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:13
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : PolishRingCommand.py
# explain   : 学习from CommandPattern.JewelCommand import JewelCommand
from CommandPattern.JewelCraftsman import JewelCraftsman# -------------------------- 3. 具体命令(Concrete Command) --------------------------
class PolishRingCommand(JewelCommand):"""抛光戒指的具体命令"""def __init__(self, craftsman: JewelCraftsman, ring_type: str):"""Args:craftsman:ring_type:"""# 绑定接收者和命令参数self.craftsman = craftsmanself.ring_type = ring_typeself.executed = False  # 标记是否已执行def execute(self):"""Returns:"""self.craftsman.polish_ring(self.ring_type)self.executed = Truedef undo(self):"""Returns:"""if self.executed:print(f"撤销操作:停止抛光 {self.ring_type} 戒指🔙")self.executed = False# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:15
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : InlayDiamondCommand.py
# explain   : 学习
from CommandPattern.JewelCommand import JewelCommand
from CommandPattern.JewelCraftsman import JewelCraftsman# -------------------------- 3. 具体命令(Concrete Command) --------------------------class InlayDiamondCommand(JewelCommand):"""镶嵌钻石的具体命令"""def __init__(self, craftsman: JewelCraftsman, necklace: str, diamond_size: str):"""Args:craftsman:necklace:diamond_size:"""self.craftsman = craftsmanself.necklace = necklaceself.diamond_size = diamond_sizeself.executed = Falsedef execute(self):"""Returns:"""self.craftsman.inlay_diamond(self.necklace, self.diamond_size)self.executed = Truedef undo(self):"""Returns:"""if self.executed:print(f"撤销操作:取出 {self.necklace} 项链上的 {self.diamond_size} 钻石🔙")self.executed = False# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:17
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : CleanNecklaceCommand.py
# explain   : 学习from CommandPattern.JewelCommand import JewelCommand
from CommandPattern.JewelCraftsman import JewelCraftsman# -------------------------- 3. 具体命令(Concrete Command) --------------------------class CleanNecklaceCommand(JewelCommand):"""清洗项链的具体命令"""def __init__(self, craftsman: JewelCraftsman, necklace: str):"""Args:craftsman:necklace:"""self.craftsman = craftsmanself.necklace = necklaceself.executed = Falsedef execute(self):"""Returns:"""self.craftsman.clean_necklace(self.necklace)self.executed = Truedef undo(self):"""Returns:"""if self.executed:print(f"撤销操作:停止清洗 {self.necklace} 项链🔙")self.executed = False

  

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:18
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : JewelWorkbench.py
# explain   : 学习from CommandPattern.JewelCommand import JewelCommand
from CommandPattern.JewelCraftsman import JewelCraftsman# -------------------------- 4. 调用者(Invoker):珠宝工作台 --------------------------
class JewelWorkbench:"""珠宝工作台(调用者:负责触发命令执行/撤销)"""def __init__(self):self.current_command = None  # 当前执行的命令self.command_history = []  # 命令执行历史(支持批量撤销/回放)def set_command(self, command: JewelCommand):"""设置要执行的命令"""self.current_command = commanddef execute_command(self):"""执行当前命令"""if self.current_command:self.current_command.execute()self.command_history.append(self.current_command)self.current_command = None  # 执行后清空当前命令else:print("没有可执行的珠宝操作命令❌")def undo_last_command(self):"""撤销上一个执行的命令"""if self.command_history:last_cmd = self.command_history.pop()last_cmd.undo()else:print("没有可撤销的珠宝操作命令❌")

  

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:命令模式(Command Pattern)
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 21:20
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : CommandBll.py
# explain   : 学习from CommandPattern.PolishRingCommand import PolishRingCommand
from CommandPattern.InlayDiamondCommand import InlayDiamondCommand
from CommandPattern.JewelWorkbench import JewelWorkbench
from CommandPattern.JewelCraftsman import JewelCraftsman
from CommandPattern.CleanNecklaceCommand import CleanNecklaceCommand# -------------------------- 5. 客户端调用示例 --------------------------class CommandBll(object):""""""def demo(self):"""Returns:"""# 1. 创建接收者(珠宝工匠)craftsman = JewelCraftsman()# 2. 创建调用者(珠宝工作台)workbench = JewelWorkbench()# 3. 创建具体命令并执行print("===== 执行第一个命令:抛光铂金戒指 =====")polish_cmd = PolishRingCommand(craftsman, "铂金")workbench.set_command(polish_cmd)workbench.execute_command()print("\n===== 执行第二个命令:给黄金项链镶嵌1克拉钻石 =====")inlay_cmd = InlayDiamondCommand(craftsman, "黄金", "1克拉")workbench.set_command(inlay_cmd)workbench.execute_command()print("\n===== 执行第三个命令:清洗珍珠项链 =====")clean_cmd = CleanNecklaceCommand(craftsman, "珍珠")workbench.set_command(clean_cmd)workbench.execute_command()# 4. 撤销操作print("\n===== 撤销最后一个命令 =====")workbench.undo_last_command()print("\n===== 再次撤销命令 =====")workbench.undo_last_command()

  

调用:

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 20:58
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : main.py
# explain   : 学习
from bll.MementoBll import MementoBll
from bll.CommandBll import CommandBll# Press the green button in the gutter to run the script.
if __name__ == '__main__':#实现备忘录模式(Memento Pattern)#mementobll= MementoBll()#mementobll.demo()#命令模式(Command Pattern)commandBll= CommandBll()commandBll.demo()print('PyCharm')

  

输出:

# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 20:58
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : main.py
# explain   : 学习
from bll.MementoBll import MementoBll
from bll.CommandBll import CommandBllif __name__ == '__main__':#实现备忘录模式(Memento Pattern)#mementobll= MementoBll()#mementobll.demo()#命令模式(Command Pattern)commandBll= CommandBll()commandBll.demo()print('PyCharm')

  

http://www.jsqmd.com/news/392533/

相关文章:

  • 人音教育网站及移动端界面设计(打造属于你的音乐学习圈)
  • 2026.2.18
  • Python Web 开发进阶实战:联邦学习优秀的平台 —— 在 Flask + Vue 中构建隐私保护的分布式 AI 训练平台
  • 一文搞懂【C++学习】十大经典排序算法全解析:原理、代码、动态图解与性能对比:核心原理+实战案例
  • 小白程序员必看:一文看懂大模型与业务流程、工作流、Agent Skills、Agentic Workflow的区别与融合之道
  • 数字员工与AI销冠系统是什么?它们为企业智能运营带来了哪些变革?
  • 普通人转行AI的真实路径
  • 燃料电池空气路建模与控制simulink模型 包括:燃料电池电堆模型(阴极,阳极,水传递
  • 2026年,哪些保健品有抗衰老功效值得关注,保健品/抗衰老片,保健品食品哪个好 - 品牌推荐师
  • 8周冲刺大模型Offer!小白转行/考研失利也能逆袭的春招秘籍_转行大模型开发了
  • C++游戏开发之旅 13
  • 裸辞转行AI大模型:小白也能看懂的成功经验与收藏指南_我的AI大模型转行记录
  • 2026全网最详细的AI大模型学习路线_大模型学习路线
  • 大模型学习路线(2026最新)神仙级大模型教程分享,不用感谢
  • 浙江宇视科技有限公司APP开发工程师(Android/iOS/鸿蒙)职位
  • 据分析转AI岗位?这4个高薪方向适配性超高,速收藏!想转岗AI大模型?
  • 安卓工程师面试题及答案
  • AI大模型学习路线(非常详细)收藏这一篇就够了!收藏这份AI大模型学习路线图,轻松入门并提升技能!
  • 掌握大数据领域数据清洗,优化数据管理流程
  • 2025年度热词Vibe Coding:小白程序员如何拥抱AI高效编程,速收藏!
  • 从氛围编程到Agent工程:GLM-5引领大模型进阶,小白必备收藏!
  • 题解:洛谷 P1904 天际线
  • 2026年江苏比较好的车铣复合培训学校排行,你知道几家?PLC培训/加工中心培训,车铣复合培训学校推荐榜单 - 品牌推荐师
  • 选择适合企业的AI Agent平台
  • 题解:洛谷 P4552 [Poetize6] IncDec Sequence
  • 题解:洛谷 P3029 [USACO11NOV] Cow Lineup S
  • 提示工程自动化测试:架构师的核心竞争力
  • 那个马云雷军的账号本质就是公共关系营销
  • 速看!2026年02月靠谱的保健品品牌推荐排行出炉,保健品/养胃颗粒/保健饮品,保健品品牌排行榜 - 品牌推荐师
  • 智能信用卡欺诈检测系统