Skip to content

Chapter 5 of 6

The SOLID Principles

The SOLID principles are five guidelines that help developers build object-oriented systems that are maintainable, flexible, testable, and resistant to gradual decay. 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 class that handles authentication, database persistence, and email sending clearly violates SRP; splitting it into a UserAuthenticator, a UserRepository, and an EmailService makes each class easier to understand, easier to test, and less tightly coupled to the others.

The Open/Closed Principle says that software entities should be open for extension but closed for modification. Instead of editing existing code to add new behavior, you should extend it through inheritance, composition, or interfaces. A common violation is adding if/else branches every time a new type appears in a system; the Strategy pattern is a typical remedy, allowing new algorithms to be plugged in without changing the context that uses them. The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without altering the correctness of the program. Subtypes must honor the supertype's contract: preconditions cannot be strengthened, postconditions cannot be weakened, and invariants must be preserved. The classic violation is Square extends Rectangle, where setting the width of a square unexpectedly changes its height.

The Interface Segregation Principle advises that clients should not be forced to depend on interfaces they do not use, preferring many small, specific interfaces over one large general-purpose one. An interface Worker that declares both work and eat methods forces every implementation, including a Robot, to provide an eat method it does not need; splitting the interface into Workable and Feedable solves the problem and reduces unnecessary dependencies.

The Dependency Inversion Principle has two parts: high-level modules should not depend on low-level modules — both should depend on abstractions — and abstractions should not depend on details, but details should depend on abstractions. In practice, instead of an OrderService depending directly on a MySQLDatabase, both depend on a DatabaseInterface that MySQLDatabase implements. DIP is a principle, while Dependency Injection is a technique for achieving it: dependencies are provided from outside rather than created internally, using constructor injection, setter injection, or interface injection. DI containers such as Spring, Laravel's service container, and the .NET dependency injection system automate this wiring. Together, the SOLID principles form a coherent foundation: SRP keeps classes focused, OCP keeps systems extensible, LSP preserves substitutability, ISP keeps interfaces lean, and DIP decouples high-level policy from low-level detail.

All chapters
  1. 1Foundations of Design Patterns
  2. 2Creational Patterns
  3. 3Structural Patterns
  4. 4Behavioral Patterns
  5. 5The SOLID Principles
  6. 6Beyond Patterns: Principles, Anti-Patterns, and Choosing Well

Drill it

Reading is not remembering. These come from the Design Patterns deck:

Q

What are Design Patterns in software engineering?

Design patterns are reusable solutions to commonly occurring problems in software design. They were popularized by the Gang of Four (GoF) in their 1994 book. Th...

Q

What are the three categories of GoF Design Patterns?

The three categories are:Creational Patterns – Deal with object creation mechanisms (e.g., Singleton, Factory Method, Builder)Structural Patterns – Deal with ob...

Q

What is the Singleton pattern?

The Singleton pattern ensures a class has only one instance and provides a global point of access to it.Key traits:Private constructorStatic method to get the i...

Q

What are the drawbacks of the Singleton pattern?

Singleton drawbacks include:Introduces global state, making code harder to testViolates the Single Responsibility Principle (manages its own lifecycle)Thread sa...