Skip to content

Chapter 2 of 8

The SOLID Design Principles

The SOLID principles, introduced by Robert C. Martin (often called Uncle Bob), are five design guidelines that together promote maintainable, flexible, and testable object-oriented software. The Single Responsibility Principle states that a class should have only one reason to change, meaning it should have just one job or responsibility. A UserService class that handles authentication, email sending, and database queries violates this principle and should be split into separate AuthService, EmailService, and UserRepository classes, each with a clear and focused purpose.

The Open/Closed Principle holds that software entities should be open for extension but closed for modification. New behavior should be added by creating new classes or methods rather than altering existing tested code. Interfaces, abstract classes, and strategy patterns are the typical tools used to enable such extensions without modifying the original code.

The Liskov Substitution Principle requires that objects of a superclass be replaceable with objects of a subclass without breaking the program. A classic violation is modeling a Square class as extending Rectangle, because setting the width on a Square unexpectedly also changes its height. The deeper rule is that subclasses must honor the behavioral contract of the parent class.

The Interface Segregation Principle argues that clients should not be forced to depend on interfaces they do not use. A fat Worker interface that declares work, eat, and sleep methods becomes problematic when a Robot class is forced to implement eating and sleeping methods it does not need. The fix is to split such interfaces into smaller, more focused ones like Workable, Eatable, and Sleepable. Finally, the Dependency Inversion Principle states that high-level modules should not depend on low-level modules; both should depend on abstractions. In an OrderService, for instance, depending on a PaymentGateway interface rather than a concrete StripePayment class makes it trivial to swap implementations.

All chapters
  1. 1The Four Pillars of OOP
  2. 2The SOLID Design Principles
  3. 3Classes, Objects, and Language Mechanics
  4. 4Object Relationships
  5. 5Polymorphism and Method Dispatch
  6. 6Abstraction Through Interfaces and Abstract Classes
  7. 7Composition, Inheritance, and Code Reuse
  8. 8General Software Design Principles

Drill it

Reading is not remembering. These come from the Oop Principles deck:

Q

What is encapsulation in OOP?

Encapsulation is the bundling of data and methods that operate on that data into a single unit (class), while restricting direct access to some components.Key b...

Q

What is inheritance in OOP?

Inheritance allows a child class (subclass) to acquire the properties and behaviors of a parent class (superclass).Example in Java:class Dog extends Animal { }B...

Q

What is polymorphism in OOP?

Polymorphism means "many forms" — the ability of an object to take on different behaviors depending on its type.Two main types:Compile-time (static): method ove...

Q

What is abstraction in OOP?

Abstraction is the process of hiding complex implementation details and exposing only the essential features of an object.Achieved through:Abstract classes — pa...