Python
設計模式
狀態模式
class A:
def __init__(self):
self.State = ''
def SetState(self, state):
self.State = state
def GetState(self):
return self.State
class State:
def ChangeState(self, obj):
pass
class StartState(State):
def ChangeState(self, obj):
print('setting Start State')
obj.SetState('Start')
class EndState(State):
def ChangeState(self, obj):
print('setting End State')
obj.SetState('End')
a = A()
S = StartState()
S.ChangeState(a)
print(a.GetState())
E = EndState()
E.ChangeState(a)
print(a.GetState())
程式碼說明
定義要被改變狀態的類別,定義state介面,定義改變狀態類別。設定物件、改變狀態。
留言
張貼留言