Single Responsibilty Principle(SRP)
This principle states that "A class should have only one reason to change"
which means every class should have a single responsibility or single job or
single purpose.
Example : Each worker has one clear job (painter paints, plumber plumbs).
4.
A single practicalproject example
(like an e-commerce app)
# ✅ S: Single Responsibility Principle
# Separate responsibilities: Product handles data, Invoice handles billing
class Product:
def __init__(self, name: str, price: float):
self.name = name
self.price = price
class Invoice:
def __init__(self, products: list[Product]):
self.products = products
def calculate_total(self):
return sum(p.price for p in self.products)
5.
Open-Closed Principle(OCP)
Thisprinciple states that "Software entities (classes, modules, functions, etc.)
should be open for extension, but closed for modification" which means you
should be able to extend a class behavior, without modifying it.
Example :
The house should allow extensions (adding a new room) but not require
breaking walls.
6.
A single practicalproject example
(like an e-commerce app)
# ✅ O: Open/Closed Principle
# Payment method can be extended without changing existing code
from abc import abstract method
class Payment(ABC):
@abstractmethod
def pay(self, amount: float):
pass
class CreditCardPayment(Payment):
def pay(self, amount: float):
print(f"Paid {amount} using Credit Card")
₹
class PayPalPayment(Payment):
def pay(self, amount: float):
print(f"Paid {amount} using PayPal")
₹
7.
Liskov's Substitution Principle(LSP)
The principle was introduced by Barbara Liskov in 1987 and according to
this principle "Derived or child classes must be sustainable for their base or
parent classes". This principle ensures that any class that is the child of a
parent class should be usable in place of its parent without any
unexpected behavior.
Example :
If you replace an electrician with another certified electrician, the work still
goes smoothly.
8.
A single practicalproject example
(like an e-commerce app)
# ✅ L: Liskov Substitution Principle
# Any new payment method can replace another without breaking functionality
def process_payment(payment: Payment, amount: float):
payment_pay(amount)
9.
Interface Segregation Principle(ISP)
It states that "do not force any client to implement an interface which is
irrelevant to them".
Example :
Don’t force a worker to carry tools they don’t need (painter doesn’t need
plumbing tools).
10.
A single practicalproject example
(like an e-commerce app)
# ✅ I: Interface Segregation Principle
# Separate interfaces for different order operations
class OrderSaver(ABC):
@abstractmethod
def save(self, invoice: Invoice):
pass
class DatabaseOrderSaver(OrderSaver):
def save(self, invoice: Invoice):
print("Order saved to database")
11.
Dependency Inversion Principle(DIP)
The Dependency Inversion Principle (DIP) is a principle in object-oriented
design that states that "High-level modules should not depend on low-level
modules. Both should depend on abstractions".
Example :
The house design depends on blueprints (abstractions), not specific workers.
In a software development team, developers depend on an abstract version
control system (e.g., Git) to manage and track changes to the codebase.
They don't depend on specific details of how Git works internally.
12.
A single practicalproject example
(like an e-commerce app)
# ✅ D: Dependency Inversion Principle
# High-level OrderService depends on abstract interfaces, not concrete classes
class OrderService:
def __init__(self, saver: OrderSaver, payment: Payment):
self.saver = saver
self.emailer = emailer
self.payment = payment
def place_order(self, invoice: Invoice):
amount = invoice.calculate_total()
self.payment.pay(amount)
self.saver.save(invoice)
print("Order successfully placed!")
13.
A single practicalproject example
(like an e-commerce app)
if __name__ == "__main__":
# Products
p1 = Product("Laptop", 60000)
p2 = Product("Mouse", 1500)
invoice = Invoice([p1, p2])
# Place Order
order_service = OrderService(
saver=DatabaseOrderSaver(),
emailer=EmailOrderNotifier(),
payment=CreditCardPayment()
)
order_service.place_order(invoice)
# Try with another payment method
order_service = OrderService(
saver=DatabaseOrderSaver(),
payment=PayPalPayment()
)
order_service.place_order(invoice)