设计模式十七(状态模式,python语言实现)
来源:互联网 发布:软件的功能界面 编辑:程序博客网 时间:2024/05/17 09:08
基本理论请参考相应书籍,这里直接给实例
基本说明:电梯(Context)内部维护着电梯的运行状态,如在几楼等信息。
state是电梯状态的的父类。子类有FloorA(一楼状态),FloorB(二楼状态)FloorC(三楼状态),FloorD(四楼状态)
客户端选择要去的楼层,电梯根据当前的状态决定是需要上行还是需要下行,并判断是否到目的地。
# -*- coding: utf-8 -*-######################################################## # state.py# Python implementation of the Class Context# Generated by Enterprise Architect# Created on: 13-十二月-2012 14:23:46# #######################################################from __future__ import divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom future_builtins import *class State(object): """This class defines an interface for encapsulating the behaviour associated with a particular state of the Context. """ m_obj=None def __init__(self, state=0): self.state=state self.current=None self.up=None self.down=None pass def Handle(self,state): if self.state==state: print('第%d' %self.state+'楼到了,请下电梯' ) State.m_obj=self pass else: self.SetContext() if self.state<state: print('正经过第%d' %self.state+'楼,电梯继续上行') self.current=self.up pass else: print('正经过第%d' %self.state+'楼,电梯继续下行') self.current=self.down pass self.current.Handle(state) pass return State.m_obj pass def SetFloor(self, floor): pass def SetContext(self): passclass FloorA(State): """This class defines an interface for encapsulating the behaviour associated with a particular state of the Context. """ def __init__(self, state=1): super(FloorA,self).__init__(state) pass def SetContext(self): self.up=FloorB() self.down=None pass def SetFloor(self, floor): passclass FloorB(State): """This class defines an interface for encapsulating the behaviour associated with a particular state of the Context. """ def __init__(self, state=2): super(FloorB,self).__init__(state) pass def SetContext(self): self.up=FloorC() self.down=FloorA() pass def SetFloor(self, floor): passclass FloorC(State): """This class defines an interface for encapsulating the behaviour associated with a particular state of the Context. """ def __init__(self, state=3): super(FloorC,self).__init__(state) pass def SetContext(self): self.up=FloorD() self.down=FloorB() pass def SetFloor(self, floor): passclass FloorD(State): """This class defines an interface for encapsulating the behaviour associated with a particular state of the Context. """ def __init__(self, state=4): super(FloorD,self).__init__(state) pass def SetContext(self): self.up=None self.down=FloorC() pass def SetFloor(self, floor): pass class Context(object): """This class defines the interface of interest to clients and maintains an instance of a ConcreteState subclass that defines the current state. """ m_State= State() def __init__(self): Context.m_State=FloorA() pass def SetState(self,state): Context.m_State=state pass def Request(self,floor): # state->Handle() print('电梯在第%d楼'%Context.m_State.state) current=Context.m_State.Handle(floor) if current!=None: self.SetState(current) pass else: print('电梯运行出错') pass print('\n') pass passif(__name__=="__main__"): #建立电梯 m_Context= Context() #到四楼 m_Context.Request(4) #然后有人到2楼 m_Context.Request(2) #然后有人到3楼 m_Context.Request(3) #然后有人到1楼 m_Context.Request(1)
运行结果:
- 设计模式十七(状态模式,python语言实现)
- 设计模式 ( 十七) 状态模式State(对象行为型)
- 设计模式 ( 十七) 状态模式State(对象行为型)
- 设计模式 ( 十七) 状态模式State(对象行为型)
- 设计模式 ( 十七) 状态模式State(对象行为型)
- 设计模式之(十七)状态模式State
- Python设计模式(十七)【观察者模式】
- C语言实现状态设计模式
- C语言实现状态设计模式
- 设计模式八(原型模式,python语言实现)
- 设计模式九(单例模式,python语言实现)
- 设计模式十(适配器模式,python语言实现)
- 设计模式十一(桥接模式,python 语言实现)
- 设计模式十二(组合模式,python语言实现)
- 设计模式十三(外观模式,python语言实现)
- 设计模式十五(命令模式,python语言实现)
- 设计模式十八(观察者模式,python语言实现)
- 设计模式十九(中介者模式,python语言实现)
- 数组 2012-12-13 15:37 pm
- 二叉树递归建立--深搜+宽搜遍历
- ubuntu 安装svn
- photoshop 改变图片颜色
- 布局demo一:listView按屏幕百分比布局
- 设计模式十七(状态模式,python语言实现)
- openmax 简介
- Heterogeneous Parallel Programming(异构并行编程)学习笔记(一)
- TLD算法matlab版本的程序调试过程
- 负载均衡VIP /LVS 的健康检测方式
- c++版本的TLD算法的编译调试
- ios设备识别
- 最简单的“系统设置隐藏多余选项”方法
- 《Windows核心编程系列》八谈谈用内核对象进行线程同步