大话设计模式(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()   



你可能感兴趣的:(python,设计模式)