SOLID
PRINCIPLES
By Jyothish Ramachandran
MCA First Year
IN OOPS
SOLID PRINCIPLES: BUILDING MAINTAINABLE AND
EXTENSIBLE CODE
Q. What is SOLID?
• Acronym for Single Responsibility, Open-Closed, Liskov Substitution,
Interface Segregation, and Dependency Inversion principles.
• A set of design principles for object-oriented programming (OOP).
02
Q. Why SOLID Matters
• Maintainability: Makes code easier to understand and modify.
• Extensibility: Promotes code that can be easily expanded without breaking
existing functionality.
• Testability: Improves code testability.
• Readability: Enhances code readability and understanding.
SOLID
PRINCIPLE
1
2
3
4
5
Single Responsibility Principle
Open-Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
SINGLE RESPONSIBILITY
PRINCIPLE (SRP)
Each module /class/ function in a competition program should be
responsible for a single, well-defined part of that program's
functionality. They should encapsulate that part and their services
should be narrowly aligned with that responsibility."
Benefits
• Maintainability: Easier to understand and modify.
• Testability: Easier to write unit tests.
• Decoupling: Reduces dependencies between classes.
A PERSON JUGGLING MULTIPLE
BALLS, STRUGGLING TO KEEP THEM
ALL IN THE AIR
OPEN-CLOSED PRINCIPLE
The Open-Closed Principle (OCP) states that software entities (like classes,
modules, or functions) should be designed in a way that allows new
functionality to be added without modifying their existing code.
Benefits
• Maintainability: Easier to add new features without modifying
existing code.
• Extensibility: Promotes flexibility and adaptability.
• Testability: Improves code testability.
LEGO BRICKS ARE MODULAR,
ALLOWING FOR EASY EXTENSION
WITHOUT MODIFYING EXISTING
PARTS.
The Liskov Substitution Principle (LSP) is a fundamental principle in object-
oriented programming (OOP) that states: if S is a subtype of T, then objects
of type T should be replaceable with objects of type S without affecting the
correctness of the program.
Benefits
• Clarity: The sentence is more concise and easier to understand.
• Specificity: The terms "subtype" and "correctness of the program" are
used to clarify the principle.
• Conciseness: The phrase "replaceable with objects of type S" is used
instead of "should be replaced with object of type S" for better clarity
and conciseness.
LISKOV SUBSTITUTION
PRINCIPLE (LSP)
A car rental company can assign you any
vehicle (bike, car, or bus), and each vehicle
should seamlessly replace another,
ensuring you can reach your destination
without altering the expected functionality.
INTERFACE SEGREGATION
PRINCIPLE (ISP)
The Interface Segregation Principle (ISP) states that clients should not be
forced to implement methods they do not use. Instead of creating a single,
large interface, multiple smaller interfaces can be defined to provide a more
granular level of functionality. This ensures that classes only implement
methods they need, reducing unnecessary coupling and improving code
maintainability.
Benefits
• Decoupling: Reduces dependencies between classes.
• Maintainability: Makes code easier to maintain and extend.
• Testability: Improves code testability.
A PERSON SORTING THROUGH A PILE
OF TRASH, SEPARATING IT INTO
DIFFERENT RECYCLING BINS LABELED
"PAPER," "PLASTIC," "GLASS," AND
"ORGANIC."
The Dependency Inversion Principle (DIP) states that high-level modules
should not depend on low-level modules. Both high-level and low-level
modules should depend on abstractions. Abstractions should not depend
on details. Details should depend on abstractions. Always strive to keep
high-level and low-level modules as loosely coupled as possible.
Benefits
• Decoupling: Reduces dependencies between modules.
• Flexibility: Makes code more flexible and adaptable to change.
• Testability: Improves code testability.
DEPENDENCY INVERSION
PRINCIPLE (DIP)
A PYRAMID WITH A WIDE BASE (LOW-LEVEL
MODULES) SUPPORTING A NARROW TOP
(HIGH-LEVEL MODULES)
TECHNICAL EXAMPLES OF EACH
PRINCIPLE
• Single Responsibility Principle (SRP)
Example: A Calculator class should only handle mathematical operations. Input validation
should be handled by a separate InputValidator class.
• Open-Closed Principle (OCP)
Example: A Shape base class can be extended with new shape types (e.g., Circle,
Rectangle, Triangle) without modifying the base class.
• Liskov's Substitution Principle
Example: Liskov Substitution Principle (LSP) Example:
If a Rectangle class is extended by a Square class, the Square should be able to
replace the Rectangle without altering the correctness of the program.
TECHNICAL EXAMPLES OF EACH
PRINCIPLE
• Interface Segregation Principle (ISP)
Example: Each application implements on smartphones hasspecific, focused
interfaces based on its functionality (e.g., a Messaging App interface for
messaging and a Camera App interface for photo and video capture), ensuring
that applications only depend on the methods they need, avoiding unnecessary
or unrelated functionality.
• Dependency Inversion Principle
Example: In a vehicle system, the Car class should depend on an
abstraction Engine rather than a concrete class like DieselEngine,
allowing easy swapping of different engine types (e.g.,
PetrolEngine, ElectricEngine).
THANK YOU

SOLID Principles in OOPS ooooooooo.pptx

  • 1.
  • 2.
    SOLID PRINCIPLES: BUILDINGMAINTAINABLE AND EXTENSIBLE CODE Q. What is SOLID? • Acronym for Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion principles. • A set of design principles for object-oriented programming (OOP). 02 Q. Why SOLID Matters • Maintainability: Makes code easier to understand and modify. • Extensibility: Promotes code that can be easily expanded without breaking existing functionality. • Testability: Improves code testability. • Readability: Enhances code readability and understanding.
  • 3.
    SOLID PRINCIPLE 1 2 3 4 5 Single Responsibility Principle Open-ClosedPrinciple Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle
  • 4.
    SINGLE RESPONSIBILITY PRINCIPLE (SRP) Eachmodule /class/ function in a competition program should be responsible for a single, well-defined part of that program's functionality. They should encapsulate that part and their services should be narrowly aligned with that responsibility." Benefits • Maintainability: Easier to understand and modify. • Testability: Easier to write unit tests. • Decoupling: Reduces dependencies between classes. A PERSON JUGGLING MULTIPLE BALLS, STRUGGLING TO KEEP THEM ALL IN THE AIR
  • 5.
    OPEN-CLOSED PRINCIPLE The Open-ClosedPrinciple (OCP) states that software entities (like classes, modules, or functions) should be designed in a way that allows new functionality to be added without modifying their existing code. Benefits • Maintainability: Easier to add new features without modifying existing code. • Extensibility: Promotes flexibility and adaptability. • Testability: Improves code testability. LEGO BRICKS ARE MODULAR, ALLOWING FOR EASY EXTENSION WITHOUT MODIFYING EXISTING PARTS.
  • 6.
    The Liskov SubstitutionPrinciple (LSP) is a fundamental principle in object- oriented programming (OOP) that states: if S is a subtype of T, then objects of type T should be replaceable with objects of type S without affecting the correctness of the program. Benefits • Clarity: The sentence is more concise and easier to understand. • Specificity: The terms "subtype" and "correctness of the program" are used to clarify the principle. • Conciseness: The phrase "replaceable with objects of type S" is used instead of "should be replaced with object of type S" for better clarity and conciseness. LISKOV SUBSTITUTION PRINCIPLE (LSP) A car rental company can assign you any vehicle (bike, car, or bus), and each vehicle should seamlessly replace another, ensuring you can reach your destination without altering the expected functionality.
  • 7.
    INTERFACE SEGREGATION PRINCIPLE (ISP) TheInterface Segregation Principle (ISP) states that clients should not be forced to implement methods they do not use. Instead of creating a single, large interface, multiple smaller interfaces can be defined to provide a more granular level of functionality. This ensures that classes only implement methods they need, reducing unnecessary coupling and improving code maintainability. Benefits • Decoupling: Reduces dependencies between classes. • Maintainability: Makes code easier to maintain and extend. • Testability: Improves code testability. A PERSON SORTING THROUGH A PILE OF TRASH, SEPARATING IT INTO DIFFERENT RECYCLING BINS LABELED "PAPER," "PLASTIC," "GLASS," AND "ORGANIC."
  • 8.
    The Dependency InversionPrinciple (DIP) states that high-level modules should not depend on low-level modules. Both high-level and low-level modules should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. Always strive to keep high-level and low-level modules as loosely coupled as possible. Benefits • Decoupling: Reduces dependencies between modules. • Flexibility: Makes code more flexible and adaptable to change. • Testability: Improves code testability. DEPENDENCY INVERSION PRINCIPLE (DIP) A PYRAMID WITH A WIDE BASE (LOW-LEVEL MODULES) SUPPORTING A NARROW TOP (HIGH-LEVEL MODULES)
  • 9.
    TECHNICAL EXAMPLES OFEACH PRINCIPLE • Single Responsibility Principle (SRP) Example: A Calculator class should only handle mathematical operations. Input validation should be handled by a separate InputValidator class. • Open-Closed Principle (OCP) Example: A Shape base class can be extended with new shape types (e.g., Circle, Rectangle, Triangle) without modifying the base class. • Liskov's Substitution Principle Example: Liskov Substitution Principle (LSP) Example: If a Rectangle class is extended by a Square class, the Square should be able to replace the Rectangle without altering the correctness of the program.
  • 10.
    TECHNICAL EXAMPLES OFEACH PRINCIPLE • Interface Segregation Principle (ISP) Example: Each application implements on smartphones hasspecific, focused interfaces based on its functionality (e.g., a Messaging App interface for messaging and a Camera App interface for photo and video capture), ensuring that applications only depend on the methods they need, avoiding unnecessary or unrelated functionality. • Dependency Inversion Principle Example: In a vehicle system, the Car class should depend on an abstraction Engine rather than a concrete class like DieselEngine, allowing easy swapping of different engine types (e.g., PetrolEngine, ElectricEngine).
  • 11.