-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
strategy_concept.py
50 lines (32 loc) · 1.03 KB
/
strategy_concept.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# pylint: disable=too-few-public-methods
"The Strategy Pattern Concept"
from abc import ABCMeta, abstractmethod
class Context():
"This is the object whose behavior will change"
@staticmethod
def request(strategy):
"""The request is handled by the class passed in"""
return strategy()
class IStrategy(metaclass=ABCMeta):
"A strategy Interface"
@staticmethod
@abstractmethod
def __str__():
"Implement the __str__ dunder"
class ConcreteStrategyA(IStrategy):
"A Concrete Strategy Subclass"
def __str__(self):
return "I am ConcreteStrategyA"
class ConcreteStrategyB(IStrategy):
"A Concrete Strategy Subclass"
def __str__(self):
return "I am ConcreteStrategyB"
class ConcreteStrategyC(IStrategy):
"A Concrete Strategy Subclass"
def __str__(self):
return "I am ConcreteStrategyC"
# The Client
CONTEXT = Context()
print(CONTEXT.request(ConcreteStrategyA))
print(CONTEXT.request(ConcreteStrategyB))
print(CONTEXT.request(ConcreteStrategyC))