-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
client.py
68 lines (47 loc) · 1.44 KB
/
client.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# pylint: disable=too-few-public-methods
"The Strategy Pattern Example Use Case"
from abc import ABCMeta, abstractmethod
class GameCharacter():
"This is the context whose strategy will change"
position = [0, 0]
@classmethod
def move(cls, movement_style):
"The movement algorithm has been decided by the client"
movement_style(cls.position)
class IMove(metaclass=ABCMeta):
"A Concrete Strategy Interface"
@staticmethod
@abstractmethod
def __call__():
"Implementors must select the default method"
class Walking(IMove):
"A Concrete Strategy Subclass"
@staticmethod
def walk(position):
"A walk algorithm"
position[0] += 1
print(f"I am Walking. New position = {position}")
__call__ = walk
class Running(IMove):
"A Concrete Strategy Subclass"
@staticmethod
def run(position):
"A run algorithm"
position[0] += 2
print(f"I am Running. New position = {position}")
__call__ = run
class Crawling(IMove):
"A Concrete Strategy Subclass"
@staticmethod
def crawl(position):
"A crawl algorithm"
position[0] += 0.5
print(f"I am Crawling. New position = {position}")
__call__ = crawl
# The Client
GAME_CHARACTER = GameCharacter()
GAME_CHARACTER.move(Walking())
# Character sees the enemy
GAME_CHARACTER.move(Running())
# Character finds a small cave to hide in
GAME_CHARACTER.move(Crawling())