0% found this document useful (0 votes)
3 views

2-SOLID Principles2

The document outlines the SOLID principles of object-oriented design in Python, which include Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. Each principle is explained with bad and good examples to illustrate proper implementation. The summary emphasizes the importance of maintaining clean, extensible, and maintainable code through these principles.

Uploaded by

ah9242117
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2-SOLID Principles2

The document outlines the SOLID principles of object-oriented design in Python, which include Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. Each principle is explained with bad and good examples to illustrate proper implementation. The summary emphasizes the importance of maintaining clean, extensible, and maintainable code through these principles.

Uploaded by

ah9242117
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SOLID Principles: Improve Object-

Oriented Design in Python

By / Samah Ezz
A class should be open for extension but closed for
2. Open/Closed modification.

Principle (OCP) Bad Example:


Modifying existing code whenever a new discount type is added.
• Good Example (Following OCP)
We use inheritance to extend
without modifying the existing class.

✅ Now, we can add more discount types without modifying


existing code.
• A subclass should be able to replace its
3. Liskov parent class without breaking the program.
Substitution • Bad Example :
Principle (LSP) • A subclass changes the expected behavior.
• Good Example (Following LSP)
We refactor by creating a separate
class for non-flying birds.

✅ Now, Penguin doesn't break the expected


behavior.
4. Interface
Segregation
Principle (ISP)

• A class shouldn't be
forced to implement
methods it doesn’t
use.
• Bad Example
(Violating ISP)
A single interface
forces all classes to
implement
unnecessary methods.
• Good Example (Following
ISP)
We split the interfaces into
smaller, specific ones.

✅ Now, Programmer doesn't have an


unnecessary eat method.
5. Dependency
Inversion Principle
(DIP)

High-level modules should depend on


abstractions, not concrete
implementations.

🔹 Bad Example (Violating DIP)

The Frontend class depends directly


on Backend, making it hard to
change the backend.
• Good Example (Following DIP)
We introduce an interface
(abstraction) for dependency
injection.

Now, we can switch Backend to another implementation without modifying Frontend.


Summary of SOLID in Python
Principle Meaning Solution

Split functionalities into


SRP One class = One responsibility
separate classes

Open for extension, closed for


OCP Use inheritance or interfaces
modification
Subclasses should work as parent Avoid breaking expected
LSP
classes behavior

No class should implement


ISP Use multiple small interfaces
unnecessary methods

Depend on abstractions, not concrete


DIP Use dependency injection
classes

You might also like