import time
class LocalStrategy:
def execute(self):
print("this is a local 104 execute")
class SaasStrategy:
def execute(self):
print("this is a saas execute")
class Switch:
def __init__(self, strategy) -> None:
self.strategy = strategy
def set_strategy(self, strategy):
self.strategy = strategy
def execute(self):
self.strategy.execute()
local_strategy = LocalStrategy()
saas_strategy = SaasStrategy()
switch = Switch(local_strategy)
for i in range(5):
if i % 2 == 0:
switch.set_strategy(saas_strategy)
else:
switch.set_strategy(local_strategy)
switch.execute()
time.sleep(1)
标签:execute,策略,self,模式,strategy,switch,local,def
From: https://www.cnblogs.com/gatling/p/18041649