"""
命令模式
"""
class Command(object):
def execute(self):
pass
class Light(object):
def __init__(self):
pass
def on(self):
print("灯开了")
class LightOnCommand(Command):
def __init__(self, light):
self.Light = light
def execute(self):
print("执行开灯命令")
self.Light.on()
class SimpleRemoteControl(object):
def __init__(self):
self.slot = None
def setCommand(self, Command):
print("设置命令")
self.slot = Command
def buttonWasPressed(self):
print('按下开关')
self.slot.execute()
class RemoteControlTest(object):
def __init__(self):
self.remote = SimpleRemoteControl()
self.light = Light()
self.lighton = LightOnCommand(self.light)
self.remote.setCommand(self.lighton)
self.remote.buttonWasPressed()
RemoteControlTest()
"""
设置命令
按下开关
执行开灯命令
灯开了
"""
headfirsh设计模式——命令模式例子
最新推荐文章于 2024-07-18 09:44:48 发布