-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_pattern.py
92 lines (58 loc) · 2.02 KB
/
factory_pattern.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Communication:
def send_notification(self):
pass
class SMSCommunication(Communication):
def send_notification(self):
pass
# do something!
class EmailCommunication(Communication):
def send_notification(self):
pass
# do something!
class SlackCommunication(Communication):
def send_notification(self):
pass
# do something!
class PhoneCommunication(Communication):
def send_notification(self):
pass
# do something!
def get_communication(is_important=False):
return SMSCommunication() if is_important else EmailCommunication()
# factoria normal
class BankAccountCommunication:
def add_money(self):
pass
def substract_money(self):
pass
# factoria abastracta
class NormalBankAccountCommunication(BankAccountCommunication):
communication: Communication = get_communication(is_important=True)
def add_money(self):
self.communication.send_notification()
def substract_money(self):
self.communication.send_notification()
class PremiumBankAccountCommunication(NormalBankAccountCommunication):
def substract_money(self):
SMSCommunication().send_notification()
class CryptoBroBankAccountCommunication(NormalBankAccountCommunication):
communication = SlackCommunication()
def get_bank_client_communication(client):
if client.cryto_bro:
return CryptoBroBankAccountCommunication()
return PremiumBankAccountCommunication() if client.premium else NormalBankAccountCommunication()
# factoria normal
class BankAccount:
def __init__(self, client):
self.communication = get_bank_client_communication(client)
def add_money(self):
pass
self.communication.add_money()
def substract_money(self):
pass
self.communication.substract_money()
class AccoutSettings:
communication: Communication = get_communication(is_important=True)
def edit_profile(self):
pass
self.communication.send_notification()