频道栏目
首页 > 资讯 > Python > 正文

设计模式(状态模式,python语言实现)

12-12-14        来源:[db:作者]  
收藏   我要投稿
基本理论请参考相应书籍,这里直接给实例
 
 
 
基本说明:电梯(Context)内部维护着电梯的运行状态,如在几楼等信息。
 
                  state是电梯状态的的父类。子类有FloorA(一楼状态),FloorB(二楼状态)FloorC(三楼状态),FloorD(四楼状态)
 
 
 
客户端选择要去的楼层,电梯根据当前的状态决定是需要上行还是需要下行,并判断是否到目的地。
 
 
 
 
[python]  
# -*- 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 division  
from __future__ import print_function  
from __future__ import unicode_literals  
from 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    www.2cto.com
            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):  
        pass  
  
  
  
class 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):  
        pass  
  
class 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):  
        pass  
  
class 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):  
        pass  
  
class 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  
    pass  
  
if(__name__=="__main__"):    
    #建立电梯  
    m_Context= Context()  
    #到四楼   
    m_Context.Request(4)  
    #然后有人到2楼       
    m_Context.Request(2)  
    #然后有人到3楼  
    m_Context.Request(3)  
    #然后有人到1楼  
    m_Context.Request(1)   
运行结果:
相关TAG标签
上一篇:Oracle参数shared_pool_size
下一篇:KUKE库客音乐图书馆过滤不严上传漏洞权限设置不当
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站