设计模式十五(命令模式,python语言实现)
来源:互联网 发布:淘宝百度云盘账号购买 编辑:程序博客网 时间:2024/05/29 15:52
基本原理请参考相应书籍,这里直接给实例
被控对象是机器人Robot,人(Client),通过控制器(controller)编程,给机器人发送指令。
Comand是指令的父类,具体指令有GoAhead(前进),GoBack(后退),GoLeft(向左),GoRight(向右)
# -*- coding: utf-8 -*-######################################################## # command.py# Python implementation of the Class Client# Generated by Enterprise Architect# Created on: 12-十二�012 16:06:11# #######################################################from __future__ import divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom future_builtins import *class Robot: """This class knows how to perform the operations associated with carrying out a request. """ def Move(self, direction,step): print("I am robot,now "+direction+" by:%d step"%step ) pass class Command(object): """This class declares an interface for executing an operation. """ m_Robot= Robot() def __init__(self, direction="None", step="None"): self.direction=direction self.step=step pass def Execute(): passclass GoAhead(Command): """This class (a) defines a binding between a Receiver object and an action, and (b) implements Execute by invoking the corresponding operation(s) on Receiver. """ def __init__(self, direction='GoAhead', step=1): super(GoAhead,self).__init__(direction,step) pass def Execute(self): # receiver->Action(); Command.m_Robot.Move(self.direction,self.step) passclass GoBack(Command): """This class (a) defines a binding between a Receiver object and an action, and (b) implements Execute by invoking the corresponding operation(s) on Receiver. """ def __init__(self, direction='GoBack', step=1): super(GoBack,self).__init__(direction,step) pass def Execute(self): # receiver->Action(); Command.m_Robot.Move(self.direction,self.step) passclass GoLeft(Command): """This class (a) defines a binding between a Receiver object and an action, and (b) implements Execute by invoking the corresponding operation(s) on Receiver. """ def __init__(self, direction='GoLeft', step=1): super(GoLeft,self).__init__(direction,step) pass def Execute(self): # receiver->Action(); Command.m_Robot.Move(self.direction,self.step) passclass GoRight(Command): """This class (a) defines a binding between a Receiver object and an action, and (b) implements Execute by invoking the corresponding operation(s) on Receiver. """ def __init__(self, direction='GoRight', step=1): super(GoRight,self).__init__(direction,step) pass def Execute(self): # receiver->Action(); Command.m_Robot.Move(self.direction,self.step) pass class Controller(object): """This class asks the command to carry out the request. """ m_Command= Command() def __init__(self): self.ls=list() pass def AddOperation(self, command): self.ls.append(command) pass def Execute(self): for i in xrange(len(self.ls)): item=self.ls.pop(0) item.Execute() pass#客户端class Client: """This class creates a ConcreteCommand object and sets its receiver. """ m_Controller= Controller() m_Controller.AddOperation(GoAhead(step=12)) m_Controller.AddOperation(GoBack(step=4)) m_Controller.AddOperation(GoLeft(step=3)) m_Controller.AddOperation(GoRight(step=5)) m_Controller.Execute() print("\n") m_Controller.AddOperation(GoAhead(step=20)) m_Controller.AddOperation(GoBack(step=44)) m_Controller.AddOperation(GoLeft(step=34)) m_Controller.AddOperation(GoRight(step=50)) m_Controller.Execute()
运行结果:
- 设计模式十五(命令模式,python语言实现)
- 设计模式(十五) 命令模式
- 设计模式之(十五)--命令模式(Conmmand)
- 设计模式学习笔记十五(Command命令模式)
- 行为型:设计模式之命令模式(十五)
- 设计模式笔记十五:命令模式
- 设计模式八(原型模式,python语言实现)
- 设计模式九(单例模式,python语言实现)
- 设计模式十(适配器模式,python语言实现)
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式十二(组合模式,python语言实现)
- 设计模式十三(外观模式,python语言实现)
- 设计模式十七(状态模式,python语言实现)
- 设计模式十八(观察者模式,python语言实现)
- 设计模式十九(中介者模式,python语言实现)
- Python设计模式(十五)【访问者模式】
- 设计模式实现(十五)--- 组合模式(Composite)
- 设计模式C++实现十五:组合模式
- unity 2.0 interception 学习2
- 服务器端获取外网ip
- linux硬件检测
- C语言字节对齐
- 网络层次协议和结构体系之间的关系
- 设计模式十五(命令模式,python语言实现)
- jQuery.Validate(Example)
- c 复制二进制流
- DIV+CSS网页布局常用的一些基础知识整理
- 项目经理问:为什么总是只有我在加班 – 挂包袱现象
- SCI EI 收录
- 查看linux版本的三种常用方法
- 人生还有四件大事不能糊涂
- 每个字符出现的个数