Python设计模式之命令模式简单示例

yipeiwu_com5年前Python基础

本文实例讲述了Python设计模式之命令模式。分享给大家供大家参考,具体如下:

命令模式介绍:

在面向对象编程中,命令模式是概括所有方法信息的设计模式。

此模式对象包涵方法名,及其相关参数值。

命令模式是一个分类的观察者设计模式,在命令模式下,对象被概括为一个命令表单,此表单包涵了所有用户需要的方法。

举个例子:如果有个按钮是用户接口“red”,当被触碰的时候,会启动后台的“turn red”接口。现在用户并不知道,通过什么类或者方法的接口能够让后台处理“turn red”操作,但是这个命令被发送了(触碰“red”按钮),会使得后台处理“turn red”操作。因此,命令模式给用户一个接口,而不用让用户了解哪些是实际执行的程序,也不会影响到用户程序。

实现命令模式的关键就是让调用者不要包涵底层实际命令执行代码,相同的调用者应该采用相同的接口。

命令模式是由三个组件构成,客户,调用者,接受者。

客户:一个实例化的对象

调用者:决定哪个方法被调用

接受者:实际命令的执行者

Example:

实现一个开关
切换ON/OFF
用开关ON/OFF去硬编码一个事件

代码如下:

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("****")

运行结果:

总结:面向对象的方法,就是这么牛叉啊,代码看得让人头晕,层层的封装。警惕面向对象编程的过度对象化。

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python中print函数简单使用总结

Python中print函数简单使用总结

print函数是Python的入门,每一个学习python的人都绕不开这个函数,下面介绍一下这个函数的用法。 打开电脑,选择python软件,下面选择python 3.7为例进行介绍,点...

Python设计模式之中介模式简单示例

Python设计模式之中介模式简单示例

本文实例讲述了Python设计模式之中介模式。分享给大家供大家参考,具体如下: Mediator Pattern:中介模式 中介模式提供了一系列统一的系统接口。此模式也被认为是行为模式,...

python多维数组切片方法

1、数组a第0个元素(二维数组)下的所有子元素(一维数组)的第一列 import numpy as np b=np.arange(24) a=b.reshape(2,3,4) pri...

Python Collatz序列实现过程解析

编写一个名为 collatz()的函数,它有一个名为 number 的参数。如果参数是偶数,那么 collatz()就打印出 number // 2, 并返回该值。如果 number 是...

python中time库的实例使用方法

time是python中处理时间的标准库 计算机时间的表达 提供获取系统时间并格式化输出功能 提供系统级精确计时功能,用于程序性能分析 用法:import time 函...