-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecorator.py
66 lines (45 loc) · 1.61 KB
/
Decorator.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
class Attack():
def calc_dmg(self) -> int:
pass
class ConcreteAttack(Attack):
_dmg = 10
def calc_dmg(self) -> int:
return self._dmg
class Decorator(Attack):
_dmg: int = 0
_Attack: Attack = None
def __init__(self, Attack: Attack) -> None:
self._Attack = Attack
@property
def Attack(self) -> Attack:
return self._Attack
def calc_dmg(self) -> int:
return self._Attack.calc_dmg()
class ConcreteDecoratorHero(Decorator):
_dmg = 50
def calc_dmg(self) -> int:
return self._dmg + self.Attack.calc_dmg()
class ConcreteDecoratorPet(Decorator):
_dmg = 15
def calc_dmg(self) -> int:
return self._dmg + self.Attack.calc_dmg()
def client_code(Attack: Attack) -> int:
return Attack.calc_dmg()
if __name__ == "__main__":
normal = ConcreteAttack()
dmg = client_code(normal)
print(f"Client: normal attack! dealing {dmg} damage!")
print("\n",
"Client: Nowadays,",
"I have become a hero and raised a pet,",
"so I have an additional attack bonus.")
with_hero_bonus = ConcreteDecoratorHero(normal)
with_pet_bonus = ConcreteDecoratorPet(with_hero_bonus)
dmg = client_code(with_pet_bonus)
print(f"Client: normal attack with hero and pet bonus! dealing {dmg} damage!")
print("\n",
"Client: For even more additional attack bonus,",
"I've raised another pet.")
with_pet_bonus = ConcreteDecoratorPet(with_pet_bonus)
dmg = client_code(with_pet_bonus)
print(f"Client: normal attack with hero and double pet bonus! dealing {dmg} damage!")