实现命令模式示例,添加接收者、命令及传令官类,增强代码可读性
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
"""
|
||||
实例:
|
||||
汤宝宝成为一国之王,手下有一个传令官和三个大臣,分别是内政大臣、外交大臣和军事大臣。
|
||||
有一天,汤宝宝想要发布一个命令,传令官负责将命令传达给大臣们,大臣们根据命令的内容决定是否执行。
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
# 接收者接口
|
||||
class Receiver(ABC):
|
||||
@abstractmethod
|
||||
def perform(self):
|
||||
pass
|
||||
|
||||
def abort(self):
|
||||
pass
|
||||
|
||||
|
||||
# 内政大臣
|
||||
class HomeSecretary(Receiver):
|
||||
is_performing: bool
|
||||
|
||||
def __init__(self):
|
||||
self.is_performing = False
|
||||
|
||||
def perform(self):
|
||||
self.is_performing = True
|
||||
print("内政大臣开始执行命令")
|
||||
|
||||
def abort(self):
|
||||
self.is_performing = False
|
||||
print("内政大臣取消执行命令")
|
||||
|
||||
|
||||
# 外交大臣
|
||||
class ForeignSecretary(Receiver):
|
||||
is_performing: bool
|
||||
|
||||
def __init__(self):
|
||||
self.is_performing = False
|
||||
|
||||
def perform(self):
|
||||
self.is_performing = True
|
||||
print("外交大臣开始执行命令")
|
||||
|
||||
def abort(self):
|
||||
self.is_performing = False
|
||||
print("外交大臣取消执行命令")
|
||||
|
||||
|
||||
# 军事大臣
|
||||
class DefenseSecretary(Receiver):
|
||||
is_performing: bool
|
||||
|
||||
def __init__(self):
|
||||
self.is_performing = False
|
||||
|
||||
def perform(self):
|
||||
self.is_performing = True
|
||||
print("军事大臣开始执行命令")
|
||||
|
||||
def abort(self):
|
||||
self.is_performing = False
|
||||
print("军事大臣取消执行命令")
|
||||
|
||||
|
||||
# 命令接口
|
||||
class Command(ABC):
|
||||
@abstractmethod
|
||||
def execute(self):
|
||||
pass
|
||||
|
||||
|
||||
# 执行命令
|
||||
class performCommand(Command):
|
||||
receiver: Receiver
|
||||
|
||||
def __init__(self, receiver: Receiver):
|
||||
self.receiver = receiver
|
||||
|
||||
def execute(self):
|
||||
self.receiver.perform()
|
||||
|
||||
|
||||
# 取消命令
|
||||
class abortCommand(Command):
|
||||
receiver: Receiver
|
||||
|
||||
def __init__(self, receiver: Receiver):
|
||||
self.receiver = receiver
|
||||
|
||||
def execute(self):
|
||||
self.receiver.abort()
|
||||
|
||||
|
||||
# 传令官
|
||||
class Herald:
|
||||
commands: list[Command]
|
||||
|
||||
def __init__(self):
|
||||
self.commands = []
|
||||
|
||||
def addCommand(self, command: Command):
|
||||
self.commands.append(command)
|
||||
|
||||
def executeCommands(self):
|
||||
for command in self.commands:
|
||||
command.execute()
|
||||
self.commands.clear()
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("========= 命令模式 =========")
|
||||
# 创建接收者
|
||||
home_secretary = HomeSecretary()
|
||||
foreign_secretary = ForeignSecretary()
|
||||
defense_secretary = DefenseSecretary()
|
||||
|
||||
# 创建命令
|
||||
perform_home_command = performCommand(home_secretary)
|
||||
perform_foreign_command = performCommand(foreign_secretary)
|
||||
perform_defense_command = performCommand(defense_secretary)
|
||||
|
||||
abort_home_command = abortCommand(home_secretary)
|
||||
abort_foreign_command = abortCommand(foreign_secretary)
|
||||
abort_defense_command = abortCommand(defense_secretary)
|
||||
|
||||
# 创建传令官
|
||||
herald = Herald()
|
||||
|
||||
# 添加执行命令
|
||||
herald.addCommand(perform_home_command)
|
||||
herald.addCommand(perform_foreign_command)
|
||||
herald.addCommand(perform_defense_command)
|
||||
|
||||
# 执行命令
|
||||
herald.executeCommands()
|
||||
|
||||
# 添加取消命令
|
||||
herald.addCommand(abort_home_command)
|
||||
herald.addCommand(abort_foreign_command)
|
||||
herald.addCommand(abort_defense_command)
|
||||
|
||||
# 执行取消命令
|
||||
herald.executeCommands()
|
||||
|
||||
print("\n======= 命令模式结束 =======")
|
||||
Reference in New Issue
Block a user