-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.py
47 lines (36 loc) · 1.75 KB
/
adapter.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
from decimal import Decimal
class LegacyLoanRequestSystem:
def get_client_scoring(self, client_id: int) -> str:
"""
Given a specific Client, it investigates and returns the client Scoring.
It's a number from 1 to 10 that specify how profitable is the client to request
a Loan.
"""
return "Some old but necessary function that retrieves value information."
class NewLoanRequestSystem:
def is_client_enabled_for_loan(self, loan_amount: Decimal, client_id: int) -> str:
"""
Given a specific amount and a client, it returns if the client
is enabled to request a new Loan.
"""
return "A new function needed to know if the client is enabled to request it."
class LegacyLoanRequestAdapter(NewLoanRequestSystem):
"""
A class in charge of adapting and old component to an existing one, avoiding
the old component modification.
"""
def __init__(self):
self.old_system = LegacyLoanRequestSystem
def get_and_adapt_client_data(self, loan_amount: Decimal, client_id: int):
legacy_data = self.old_system().get_client_scoring(client_id)
# some operation to define whether the client can or cannot
# request the Loan with a new result expected by the system.
return legacy_data
def is_client_enabled_for_loan(self, loan_amount: Decimal, client_id: int):
return self.get_and_adapt_client_data(loan_amount, client_id)
if __name__ == "__main__":
# The client needs both opinions to define if its enabled to request the loan
loan_amount = 100_000
client_id = 2
for request_system in [NewLoanRequestSystem, LegacyLoanRequestAdapter]:
print(request_system().is_client_enabled_for_loan(loan_amount, client_id))