命令行模式
实现命令模式的关键就是让调用者不要包涵底层实际命令执行代码,相同的调用者应该采用相同的接口。
命令模式是由三个组件构成,客户,调用者,接受者。
客户:一个实例化的对象
调用者:决定哪个方法被调用
接受者:实际命令的执行者
#coding=utf-8
class Switch:
''' The INVOKER class'''
def __init__(self, flipUpCmd, flipDownCmd):
self.__flipUpCommand = flipUpCmd
self.__flipDownCommand = flipDownCmd
def flipUp(self): #执行抬起开关
self.__flipUpCommand.execute()
def flipDown(self):
self.__flipDownCommand.execute()
class Light:
'''The RECEIVER Class'''
def turnOn(self):
print "The light is on"
def turnOff(self):
print "The light is off"
class Command:
"""The Command Abstrace class"""
def __init__(self):
pass
def execute(self):
pass
class FlipUpCommand(Command):
'''The Command class for turning on the light'''
def __init__(self, light):
self.__light = light
def execute(self):
self.__light.turnOn()
class FileDownCommand(Command):
'''The Command class for turning off the light'''
def __init__(self, light):
Command.__init__(self)
self.__light = light
def execute(self):
self.__light.turnOff()
class LightSwitch: #主管
'''The Client Class'''
def __init__(self):
self.__lamp = Light()
self.__switchUp = FlipUpCommand(self.__lamp)
self.__switchDown = FileDownCommand(self.__lamp)
self.__switch = Switch(self.__switchUp, self.__switchDown)
def switch(self, cmd):
cmd = cmd.strip().upper()
try:
if cmd == "ON":
self.__switch.flipUp()
elif cmd == "OFF":
self.__switch.flipDown()
else:
print "Argument \"ON\" or \"OFF\" is required"
except Exception,msg:
print "Exception occured:%s" % msg
#Execute if the file is run as a script and not imported as a module
if __name__ == "__main__":
lightSwitch = LightSwitch()
print "Switch ON test"
lightSwitch.switch("ON") #老板发布的命令
print "Switch OFF test"
lightSwitch.switch("OFF") #老板发布的命令
print "Invalid Command test"
lightSwitch.switch("****") #老板发布的命令