python
設計模式
策略模式
class Stragey: def Algorithm(self): pass
class
Stragey1(Stragey): def Algorithm(self): print('run algorithm1')
class Stragey2(Stragey): def Algorithm(self): print('run algorithm2')
class A: def __init__(self): self.a = None def SetAlgorithm(self, algorithm): self.a = algorithm
a = A()a.SetAlgorithm(Stragey1()) a.a.Algorithm() a.SetAlgorithm(Stragey2()) a.a.Algorithm()程式碼說明
定義策略模式,寫策略一、策略二類別,定義類別A,並設定可以存放不同策略之函數,執行上述功能。
留言
張貼留言