当前位置:编程学习 > 网站相关 >>

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

基本理论请参考相应书籍,这里直接给实例
 
 
 
基本说明:电梯(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.zzzyk.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
补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,