设计模式十七(状态模式,python语言实现)
基本理论请参考相应书籍,这里直接给实例
基本说明:电梯(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) 运行结果:
