JAVA 中的命令模式大家估计都很熟悉,就是可以讲命令调用者和命令的执行者进行解耦。

看一下类图:

第一步:   目前Python设计的实体有电灯,和仓库门 (Light,GarageDoor)

Entity

 1 #coding = utf8
 2 class Light(object):
 3     def on(self):
 4         print '---the light on---'
 5     def off(self):
 6         print '---the light off---'
 7
 8 class GarageDoor(object):
 9     def action_up(self,*args):
10         print '--- the garageDoor open ---'
11     def action_down(self,*args):
12         print '---  the garageDoor down ---'
13
14     def action_stop(self,*args):
15         print '--- the garageDoor stop  ---'
16
17     def action_lighton(self,*args):
18         print '--- the garageDoor lighton ---'
19
20     def action_lightoff(self,*args):
21         print '--- the garageDoor lightoff---'

具体的命令如Light.on,Light.off 等在实体类中进行了定义。

第二步: 进行,AbstractCommand ,和ConcreteCommand 的实现。

Command

 1 class Command(object):
 2     def callback(self,invoker,name,*args):
 3         method=getattr(invoker,name,None)
 4         #print method
 5         #print args
 6         if callable(method):
 7             method(*args)
 8             #print invoker
 9
10     def setTask(self,tasks):
11         pass
12
13     def execute(self):
14         pass
15
16 class NoCommand(object):
17     def setTask(self):
18         pass
19     def execute(self):
20         pass
21
22 # the type no constraint
23 class LightOnCommand(Command):
24     def __init__(self,light):
25         self.light=light
26
27     def execute(self):
28         self.light.on()
29
30
31
32
33 class GarageDoorCommand(Command):
34
35
36     def __init__(self,garageDoor,tasks):
37         self.garageDoor = garageDoor
38         self.tasks=tasks
39
40     def setTasks(self,tasklist):
41         self.tasks=tasklist
42
43
44     def execute(self):
45         for task in self.tasks:
46             self.callback(garageDoor,task[0],task[1])   

由于Python 中的接口,和继承都在 通过标签实现的。这一点同JAVA 有很大的不同

Invoker

 1 class SimpleRemoteControl(object):
 2     def __init__(self,command):
 3         self.command=command
 4     def buttonWasPressed(self):
 5         self.command.execute()
 6
 7
 8 class RemoteControl(object):
 9      commands_open=[None for i in range(8)]
10      commands_off=[None for i in range(8)]
11
12      def __init__(self):
13        pass

最后在Invoker进行调用,其中的Light,GarageDoor 相当于Receiver 。

这整个过程类似于JAVA 中Command 模式的实现了。

Python 设计模式-- 命令模式相关推荐

  1. Python设计模式-命令模式

    Python设计模式-命令模式 代码基于3.5.2,代码如下; #coding:utf-8 #命令模式class barbecuer():def bakeButton(self):print(&quo ...

  2. Python设计模式-建造者模式

    Python设计模式-建造者模式 代码基于3.5.2,代码如下; #coding:utf-8 #建造者模式 class Burger():name = ""price = 0.0d ...

  3. Python设计模式-状态模式

    Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...

  4. Python设计模式-备忘录模式

    Python设计模式-备忘录模式 代码基于3.5.2,代码如下; #coding:utf-8 #备忘录模式 import randomclass gameCharacter():vitality = ...

  5. Python设计模式-解释器模式

    Python设计模式-解释器模式 代码基于3.5.2,代码如下; #coding:utf-8 #解释器模式class PlayContext():play_text = Noneclass Expre ...

  6. Python设计模式-策略模式

    Python设计模式-策略模式 代码基于3.5.2,代码如下; #coding:utf-8 #策略模式class sendInterface():def send(self,value):raise ...

  7. Python设计模式-外观模式

    Python设计模式-外观模式 代码基于3.5.2,代码如下; #coding:utf-8 # 外观模式class AlarmSensor:def run(self):print("Alar ...

  8. Python设计模式-桥接模式

    Python设计模式-桥接模式 基于Python3.5.2,代码如下 #coding:utf-8class Shape():name = ""param = "" ...

  9. Python设计模式-代理模式

    Python设计模式-代理模式 基于Python3.5.2,代码如下 #coding:utf-8info_struct = dict() info_struct["addr"] = ...

  10. 设计模式 命令模式 实例_根据您的命令-命令设计模式

    设计模式 命令模式 实例 命令设计模式是一种广为人知的设计模式,属于行为设计模式("四人帮"的一部分). 顾名思义,它与应用程序中的动作和事件有关. 问题陈述: 假设有一个网页将在 ...

最新文章

  1. 对textfield的键盘改造成滚动选择器而不是输入键盘
  2. android onpause时动画出错,Android生命周期之onPauseonStop
  3. FreeRTOS — 临界段和开关中断
  4. 2019年终总结--满城灯火,只若初见
  5. 产品设计丨价格理论与用户体验
  6. 生日小助手的详细规划——本博文随时更新,持续有效……
  7. 深度学习笔记(1) 深度学习简介
  8. S7-300/400 系统 Profibus DP 通讯故障诊断实用方
  9. apipost预执行脚本中发送一个请求的方法
  10. Redis集群案例与场景分析
  11. PostgreSQL【模式Schema】
  12. python地理数据处理相关的操作
  13. altium designer 2019安装教程
  14. python局域网传输文件
  15. 网络攻防技术——缓冲区溢出攻击实验
  16. 关于HTTP GZIP解压问题
  17. 欧若拉用计算机谱子,欧若拉简谱
  18. 《动手学深度学习》(PyTorch版)代码注释 - 55 【Text_sentiment_classification(textCNN)】
  19. DNS服务器无响应win10系统,Win10上的DNS服务器无响应:这是如何解决它
  20. 2022年京东618活动规则:618满减规则为299减50

热门文章

  1. iOS 提交app到iTunes Connect预览截图截取方法及尺寸大小
  2. 利用Struts拦截器完成文件上传功能
  3. java_if_else__的应用1
  4. 13、第七 - 网络编程基础 - Python中协程EVENT()函数-事件函数
  5. linux C 总结篇(线程)下
  6. 通用权限底层研究:强大的分页功能
  7. powershell玩转ExcelCSV(一)
  8. js关于两个字符串的加减乘除运算
  9. SpringMvc定时器任务
  10. 使用Gogs搭建Git服务器