大话设计模式(Python版)--状态模式

工作状态--函数版

#!/usr/bin/env python

def WriteProgram(hour, workFinished=False):
    if(hour < 12):
        print("current time: ",hour,' work at morning, spirit')
    elif(hour < 13):
        print("current time: ",hour,' work at noon, sleepy')
    elif(hour < 17):
        print("current time: ",hour,' work at afternoon, spirit')
    else:
        if(workFinished):
            print("current time: ",hour,' leave out of office.')
        else:
            if(hour < 21):
                print("current time: ",hour,' work overtime, very tired')
            else:
                print("current time: ",hour,' canot work anymore, sleep')
   
def main():
    hour = 9
    WriteProgram(hour)
    hour = 10
    WriteProgram(hour)    
    hour = 12
    WriteProgram(hour)                
    hour = 14
    WriteProgram(hour)        
    hour = 16
    WriteProgram(hour)        
    hour = 18
    WriteProgram(hour)        
    hour = 21
    WriteProgram(hour)    

if __name__ == '__main__':
    main()

工作状态--分类版

#!/usr/bin/env python

class work:
    @property
    def Hour(self):
        return self.__hour
    @Hour.setter
    def Hour(self,value):
        self.__hour = value
    @property
    def TaskFinished(self):
        self.__finish = False
    @TaskFinished.setter
    def TaskFinished(self,value):
        self.__finish = value
    def WriteProgram(self):
        if(self.__hour < 12):
            print("current time: ",self.__hour,' work at morning, spirit')
        elif(self.__hour < 13):
            print("current time: ",self.__hour,' work at noon, sleepy')
        elif(self.__hour < 17):
            print("current time: ",self.__hour,'work at afternoon, spirit')        
        else:
            if(self.__finish ):
                print("current time: ",self.__hour,' leave out of office.')
            else:
                if(self.__hour < 21):
                    print("current time: ",self.__hour,' work overtime, very tired')
                else:
                    print("current time: ",self.__hour,' canot work anymore, sleep')
                
def main():
    wk = work()
    wk.Hour = 9
    wk.WriteProgram()
    wk.Hour = 10
    wk.WriteProgram()
    wk.Hour = 12
    wk.WriteProgram()
    wk.Hour = 14
    wk.WriteProgram()
    wk.Hour = 17
    wk.TaskFinished = False
    wk.WriteProgram()
    wk.Hour = 20
    wk.WriteProgram()    
    wk.Hour = 22
    wk.WriteProgram()         
                    
if __name__ == '__main__':
    main()
    
状态模式版--v1

#!/usr/bin/env python
from abc import abstractmethod
class State:
    @abstractmethod
    def Handle(self,context):
        pass

class ConcreteStateA(State):
    def Handle(self,context):
        context.State = ConcreteStateB()

class ConcreteStateB(State):
    def Handle(self,context):
        context.State = ConcreteStateA()

class Context:
    def __init__(self, state):
        self.__state = state
    @property
    def State(self):
        return self.__state
    @State.setter
    def State(self,value):
        self.__state = value
        print("current state is ")
    def Request(self):
        self.__state.Handle(self)

def main():
    ct = Context(ConcreteStateA())
    ct.Request()
    ct.Request()
    ct.Request()
    ct.Request()

if __name__ == '__main__':
    main()

工作状态--状态模式版

#!/usr/bin/env python
from abc import abstractmethod

class State:
    @abstractmethod
    def WriteProgram(self, work):
        pass

class ForenoonState(State):
    def WriteProgram(self, work):
        if( work.Hour < 12):
            print("current time: ",work.Hour,' work at morning, spirit')
        else:
            work.SetState(NoonState())
            work.WriteProgram()

class NoonState(State):
    def WriteProgram(self, work):
        if( work.Hour < 13):
            print("current time: ",work.Hour,' dinner time, hungery')
        else:
            work.SetState(AfternoonState())
            work.WriteProgram()
        
class AfternoonState(State):
    def WriteProgram(self, work):
        if( work.Hour < 17):
            print("current time: ",work.Hour,' work at afternoon, spirit')
        else:
            work.SetState(EveningState())
            work.WriteProgram()

class EveningState(State):
    def WriteProgram(self, work):
        if( work.TaskFinished):
            work.SetState(ResetState())
            work.WriteProgram()
        else:
            if(work.Hour < 12):
               print("current time: ",work.Hour,' work overtime, very tired')
            else:
                work.SetState(SleepingState())
                work.WriteProgram()
                
class SleepingState(State):
    def WriteProgram(self, work):
        print("current time: ",work.Hour,' canot work anymore, sleep')

class ResetState(State):
    def WriteProgram(self, work):
        print("current time: ",work.Hour,' it is time to go home')

class Work:
    def __init__(self):
        self.current = ForenoonState()
    @property
    def Hour(self):
        return self.__hour
    @Hour.setter
    def Hour(self,value):
        self.__hour = value
    @property
    def TaskFinished(self):
        return self.__finish
    @TaskFinished.setter
    def TaskFinished(self,value):
        self.__finish = value
    def SetState(self, state):
        self.current = state
    def WriteProgram(self):
        self.current.WriteProgram(self)

def main():
    wk = Work()
    wk.Hour = 9
    wk.WriteProgram()
    wk.Hour = 10
    wk.WriteProgram()
    wk.Hour = 12
    wk.WriteProgram()
    wk.Hour = 14
    wk.WriteProgram()
    wk.Hour = 17
    wk.TaskFinished = False
    wk.WriteProgram()
    wk.Hour = 20
    wk.WriteProgram()    
    wk.Hour = 22
    wk.WriteProgram()

if __name__ == '__main__':
    main()   



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值