-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_closed_principle.py
41 lines (26 loc) · 1009 Bytes
/
open_closed_principle.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
from abc import ABC, abstractmethod
# Define an abstract base class for Customer
class Customer(ABC):
@abstractmethod
def get_discount(self, amount):
pass
# RegularCustomer class implementing get_discount
class RegularCustomer(Customer):
def get_discount(self, amount):
return amount * 0.1 # 10% discount
# VIPCustomer class implementing get_discount
class VIPCustomer(Customer):
def get_discount(self, amount):
return amount * 0.2 # 20% discount
# DiscountCalculator class using Customer interface
class DiscountCalculator:
def calculate_discount(self, customer, amount):
return customer.get_discount(amount)
# Usage example
calculator = DiscountCalculator()
print("\n\n")
discount = calculator.calculate_discount(RegularCustomer(), 100)
print(f"Discount for RegularCustomer on amount 100: {discount}")
discount = calculator.calculate_discount(VIPCustomer(), 100)
print(f"Discount for VIPCustomer on amount 100: {discount}")
print("\n\n")