generated from grellert/ine5404-aula-03-nov-solid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.ocp.py
69 lines (50 loc) · 1.59 KB
/
2.ocp.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
"""
Open-Closed Principle
Classes devem estar fechadas para modificação, mas abertas para extensão
"""
from abc import ABC, abstractclassmethod
class Animal:
def __init__(self, name: str, sound: str):
self.name = name
self.sound = sound
def get_name(self) -> str:
pass
def make_sound(self):
print(self.sound)
animals = [
Animal('lion', 'roar'),
Animal('mouse', 'squeak')
]
def animal_sound(animals: list):
for animal in animals:
animal.make_sound()
animal_sound(animals)
"""
Outro exemplo:
Imagine que você tem uma loja que dá desconto de 20% aos seus clientes favoritos
usando essa classe abaixo. Quando você decide dar 40% de desconto a clientes VIP,
você decide mudar a classe da seguinte forma:
"""
class Discount(ABC):
def __init__(self, customer, price):
self.customer = customer
self.price = price
@abstractclassmethod
def give_discount(self):
pass
class DiscountVip(Discount):
def __init__(self, customer, price):
if customer == "vip":
super().__init__(customer, price)
else:
raise TypeError("DiscountVip só é permitido para clientes do tipo vip")
def give_discount(self):
return self.price * 0.4
class DiscountFav(Discount):
def __init__(self, customer, price):
if customer == "fav":
super().__init__(customer, price)
else:
raise TypeError("DiscountFav só é permitido para clientes do tipo fav")
def give_discount(self):
return self.price * 0.2