您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何用不同于Wikipedia中示例的方式在Python中编写策略模式?

如何用不同于Wikipedia中示例的方式在Python中编写策略模式?

Python中的示例与其他示例并无太大不同。模拟PHP脚本:

class StrategyExample:
    def __init__(self, func=None):
        if func:
             self.execute = func

    def execute(self):
        print("Original execution")

def executeReplacement1():
    print("Strategy 1")

def executeReplacement2():
    print("Strategy 2")

if __name__ == "__main__":
    strat0 = StrategyExample()
    strat1 = StrategyExample(executeReplacement1)
    strat2 = StrategyExample(executeReplacement2)

    strat0.execute()
    strat1.execute()
    strat2.execute()

输出

Original execution
Strategy 1
Strategy 2

主要区别在于:

请注意,有3种方法可以在Python中动态添加方法

我给你看的方式 但是该方法将是静态的,不会传递“ self”参数。

使用类名:

StrategyExample.execute = func

在这里,所有实例都将func作为execute方法,并self作为参数传递。

strat0.execute = types.MethodType(executeReplacement1, strat0)

或使用Python 2,还需要更改实例的类:

strat0.execute = types.MethodType(executeReplacement1, strat0, StrategyExample)

与第一个示例一样,这会将新方法绑定到strat0和only strat0。但是start0.execute()self以争论的形式通过。

如果需要在函数中使用对当前实例的引用,则可以合并第一个和最后一个方法。如果你不:

class StrategyExample:
    def __init__(self, func=None):
        self.name = "Strategy Example 0"
        if func:
             self.execute = func

    def execute(self):
        print(self.name)

def executeReplacement1():
    print(self.name + " from execute 1")

def executeReplacement2():
    print(self.name + " from execute 2")

if __name__ == "__main__":
    strat0 = StrategyExample()
    strat1 = StrategyExample(executeReplacement1)
    strat1.name = "Strategy Example 1"
    strat2 = StrategyExample(executeReplacement2)
    strat2.name = "Strategy Example 2"

    strat0.execute()
    strat1.execute()
    strat2.execute()

你会得到:

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    strat1.execute()
  File "test.py", line 13, in executeReplacement1
    print self.name + " from execute 1"
NameError: global name 'self' is not defined

因此正确的代码将是:

import sys
import types

if sys.version_info[0] > 2:  # Python 3+
    create_bound_method = types.MethodType
else:
    def create_bound_method(func, obj):
        return types.MethodType(func, obj, obj.__class__)

class StrategyExample:
    def __init__(self, func=None):
        self.name = "Strategy Example 0"
        if func:
             self.execute = create_bound_method(func, self)

    def execute(self):
        print(self.name)

def executeReplacement1(self):
    print(self.name + " from execute 1")

def executeReplacement2(self):
    print(self.name + " from execute 2")

if __name__ == "__main__":
    strat0 = StrategyExample()
    strat1 = StrategyExample(executeReplacement1)
    strat1.name = "Strategy Example 1"
    strat2 = StrategyExample(executeReplacement2)
    strat2.name = "Strategy Example 2"

    strat0.execute()
    strat1.execute()
    strat2.execute()

这将输出预期的结果:

Strategy Example 0
Strategy Example 1 from execute 1
Strategy Example 2 from execute 2

当然,在这功能不能再单独使用的情况下,但是仍然可以绑定到任何对象的任何其他实例,而没有任何接口限制。

python 2022/1/1 18:46:47 有316人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶