设计模式十七(状态模式,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 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 
            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) 

运行结果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值