Python中的策略模式:如何处理从一种策略到另一种策略的价值?

时间:2018-11-29 16:02:49

标签: python strategy-pattern

我需要您的帮助,以了解如何在Python中正确使用策略模式。
确实,我不知道如何将一项策略的结果转移到另一项。

结构为:(文件在这篇文章的底部)

  • App.py
  • AStrategyAbstract.py
  • BStrategyAbstract.py
  • CStrategyAbstract.py

运行下面的应用, App.py 给我以下内容:

A1 Strategy applied
Give me your name
Foo !
B3 Strategy applied
C2 Strategy applied
___
A2 Strategy applied
Give me your name
Bar !
B1 Strategy applied
C2 Strategy applied

我想了解以下内容,但我不知道如何

A1 Strategy applied
Give me your name
Foo !
B3 Strategy applied
Name given was Foo!
C2 Strategy applied
___
A2 Strategy applied
Give me your name
Bar !
B1 Strategy applied
Name given was Bar !
C2 Strategy applied

能请别人指出我正确的方向吗?

关于

皮埃尔


文件名:App.py

from AStrategyAbstract import A1Strategy,A2Strategy,A3Strategy
from BStrategyAbstract import B1Strategy,B2Strategy,B3Strategy
from CStrategyAbstract import C1Strategy,C2Strategy,C3Strategy


a1_strategy = A1Strategy()
a2_strategy = A2Strategy()
a3_strategy = A3Strategy()


b1_strategy = B1Strategy()
b2_strategy = B2Strategy()
b3_strategy = B3Strategy()


c1_strategy = C1Strategy()
c2_strategy = C2Strategy()
c3_strategy = C3Strategy()

class App(object):
    def __init__(self, a_strategy, b_strategy, c_strategy):
        self._a_strategy = a_strategy
        self._b_strategy = b_strategy
        self._c_strategy = c_strategy
        self.name = None

    def do_sth(self):
        self._a_strategy.do_sth()
        self._b_strategy.do_sth()
        self._c_strategy.do_sth()

class AApp(App):
    def __init__(self):
        super(AApp, self).__init__(a1_strategy, b3_strategy, c2_strategy)

    def start(self):
        self.do_sth()

class BApp(App):
    def __init__(self):
        super(BApp, self).__init__(a1_strategy, b1_strategy, c2_strategy)

    def start(self):
        self.do_sth()

if __name__ == '__main__':
    myaapp = AApp()   
    myaapp.start()
    print("___")
    mybapp = BApp()   
    mybapp.start()

文件名:AStrategyAbstract.py

import abc 

class AStrategyAbstract(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def do_sth(self):
        """Required Method"""

class A1Strategy(AStrategyAbstract):
    def do_sth(self):
        print("A1 Strategy applied")
        print("Give me your name")
        name = input()

class A2Strategy(AStrategyAbstract):
    def do_sth(self):
        print("A2 Strategy applied")
        print("Give me your name")
        name = input()

class A3Strategy(AStrategyAbstract):
    def do_sth(self):
        print("A3 Strategy applied")
        print("Give me your name")
        name = input()

文件名:BStrategyAbstract.py

import abc 

class BStrategyAbstract(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def do_sth(self):
        """Required Method"""

class B1Strategy(BStrategyAbstract):
    def do_sth(self):
        print("B1 Strategy applied")

class B2Strategy(BStrategyAbstract):
    def do_sth(self):
        print("B2 Strategy applied")

class B3Strategy(BStrategyAbstract):
    def do_sth(self):
        print("B3 Strategy applied")

文件名:CStrategyAbstract.py

import abc 

class CStrategyAbstract(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def do_sth(self):
        """Required Method"""

class C1Strategy(CStrategyAbstract):
    def do_sth(self):
        print("C1 Strategy applied")

class C2Strategy(CStrategyAbstract):
    def do_sth(self):
        print("C2 Strategy applied")

class C3Strategy(CStrategyAbstract):
    def do_sth(self):
        print("C3 Strategy applied")

0 个答案:

没有答案