我希望创建一个类,使用与以下类似的策略设计模式:
如果我去掉
此外,是否有更好的方法来公开策略,而不是作为静态方法?我认为在这里实现完整的类没有意义。
class C:
@staticmethod
def default_concrete_strategy():
print("default")
@staticmethod
def other_concrete_strategy():
print("other")
def __init__(self, strategy=C.default_concrete_strategy):
self.strategy = strategy
def execute(self):
self.strategy()
以下是出现的错误:
NameError: name 'C' is not defined
将strategy=C.default_concrete_strategy
替换为strategy=default_concrete_strategy
可以正常工作,但如果保留默认设置,策略实例变量将是一个静态方法的对象而不是可调用的方法。
TypeError: 'staticmethod' object is not callable
如果我去掉
@staticmethod
装饰器,它将正常工作。但是,还有其他方法吗?我希望默认参数可以记录自身,以便其他人可以立即看到如何包含策略的示例。此外,是否有更好的方法来公开策略,而不是作为静态方法?我认为在这里实现完整的类没有意义。