Skip to content

Instantly share code, notes, and snippets.

@gusibi
Created January 9, 2018 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusibi/e66134218fdecff59e5690298d657c26 to your computer and use it in GitHub Desktop.
Save gusibi/e66134218fdecff59e5690298d657c26 to your computer and use it in GitHub Desktop.
python设计模式-命令模式
#! -*- coding: utf-8 -*-
"""
命令模式
"""
class Command(object):
def execute(self):
raise NotImplementedError()
def undo(self):
raise NotImplementedError()
class NoCommand(Command):
def execute(self):
print('Command Not Found')
def undo(self):
print('Command Not Found')
class Light(object):
def __init__(self, name):
self.name = name
def on(self):
print('%s Light is On' % self.name)
def off(self):
print('%s Light is Off' % self.name)
class LightOnCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.light.on()
def undo(self):
self.light.off()
class LightOffCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.light.off()
def undo(self):
self.light.on()
class GarageDoor(object):
def open(self):
print('Garage Door is Open')
def close(self):
print('Garage Door is Close')
class GarageDoorOpenCommand(Command):
def __init__(self, door):
self.door = door
def execute(self):
self.door.open()
def undo(self):
self.door.close()
class GarageDoorCloseCommand(Command):
def __init__(self, door):
self.door = door
def execute(self):
self.door.close()
def undo(self):
self.door.open()
class MacroCommand(Command):
def __init__(self, commands):
self.commands = commands
def execute(self):
for command in self.commands:
command.execute()
def undo(self):
for command in self.commands:
command.undo()
class RemoteControl(object):
def __init__(self):
self.on_commands = [NoCommand() for i in range(7)]
self.off_commands = [NoCommand() for i in range(7)]
self.undo_command = None
def set_command(self, slot, on_command, off_command):
# 预先给每个插槽设置一个空命令的命令
self.on_commands[slot] = on_command
self.off_commands[slot] = off_command
def on_button_was_pressed(self, slot):
command = self.on_commands[slot]
command.execute()
self.undo_command = command
def off_button_was_pressed(self, slot):
command = self.off_commands[slot]
command.execute()
self.undo_command = command
def undo_button_was_pressed(self):
self.undo_command.undo()
def __str__(self):
for i in range(7):
print('[slot %d] %s %s' % (i,
self.on_commands[i].__class__.__name__,
self.off_commands[i].__class__.__name__))
return ''
def remote_control_test():
remote = RemoteControl()
living_room_light = Light('Living Room')
kitchen_light = Light('Kitchen')
garage_door = GarageDoor()
living_room_light_on = LightOnCommand(living_room_light)
living_room_light_off = LightOffCommand(living_room_light)
kitchen_light_on = LightOnCommand(kitchen_light)
kitchen_light_off = LightOffCommand(kitchen_light)
garage_door_open = GarageDoorOpenCommand(garage_door)
garage_door_close = GarageDoorCloseCommand(garage_door)
remote.set_command(0, living_room_light_on, living_room_light_off)
remote.set_command(1, kitchen_light_on, kitchen_light_off)
remote.set_command(2, garage_door_open, garage_door_close)
print(remote)
remote.on_button_was_pressed(0)
remote.off_button_was_pressed(0)
remote.undo_button_was_pressed()
remote.on_button_was_pressed(1)
remote.off_button_was_pressed(1)
remote.undo_button_was_pressed()
remote.on_button_was_pressed(2)
remote.off_button_was_pressed(2)
remote.undo_button_was_pressed()
# 测试开关集合
party_on_macro = MacroCommand([living_room_light_on, kitchen_light_on])
party_off_macro = MacroCommand([living_room_light_off, kitchen_light_off])
remote.set_command(3, party_on_macro, party_off_macro)
print('--pushing macro on--')
remote.on_button_was_pressed(3)
print('--pushing macro off--')
remote.off_button_was_pressed(3)
print('--push macro undo--')
remote.undo_button_was_pressed()
if __name__ == '__main__':
remote_control_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment