SOLID Principle in software design example

deepalishinkar1 24 views 14 slides Aug 29, 2025
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

These slides explained 5 principle of software design.


Slide Content

DESIGN PRINCIPLES AND PATTERNS SOLID PRINCIPLES

SOLID Principles

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).

A single practical project 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)

Open-Closed Principle(OCP) This principle 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.

A single practical project 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")

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.

A single practical project 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)

  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).

A single practical project 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")

  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 .

A single practical project 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!")

A single practical project 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)

Summary
Tags