The 5 SOLID Principles of Object-Oriented Design

Sampath Dissanayake
3 min readDec 11, 2022

--

The SOLID principles are a set of five fundamental design principles for object-oriented programming (OOP). These principles were first introduced by Robert C. Martin, also known as “Uncle Bob,” in his 2000 paper “Design Principles and Design Patterns.” The SOLID principles provide a framework for designing software that is easy to maintain, extend, and test.

Uncle Bob AKA Robert C Martin

The acronym SOLID stands for the five principles:

  1. Single Responsibility Principle: A class should have only one reason to change.
  2. Open/Closed Principle: A class should be open for extension but closed for modification.
  3. Liskov Substitution Principle: Subclasses should be substitutable for their base classes.
  4. Interface Segregation Principle: Clients should not be forced to implement interfaces they don’t use.
  5. Dependency Inversion Principle: High-level modules should not depend on low-level modules; both should depend on abstractions.

The Single Responsibility Principle (SRP) states that a class should have only one reason to change. This means that a class should have only one responsibility or job, and that responsibility should be completely encapsulated by the class. This helps to keep the design of the software simple and maintainable. For example, consider a Customer class that has methods for storing and retrieving customer information. If this class also had a method for sending emails to customers, it would violate the SRP because the responsibility of sending emails is not directly related to the responsibility of storing and retrieving customer information.

The Open/Closed Principle (OCP) states that a class should be open for extension but closed for modification. This means that a class should be designed in such a way that new functionality can be added to it without modifying the existing code. This is achieved by using inheritance and polymorphism, which allow new subclasses to be created that extend the functionality of the base class without modifying it. For example, consider a Shape class that has a draw() method. If we want to add a new shape, such as a Triangle, we can create a Triangle subclass that extends the Shape class and overrides the draw() method to draw a triangle.

The Liskov Substitution Principle (LSP) states that subclasses should be substitutable for their base classes. This means that if a program is written using a base class, it should be able to use any of its subclasses without knowing the specific type of the object being used. This is achieved by defining a common interface for the base class and its subclasses, and by ensuring that the subclasses adhere to that interface. For example, consider a Vehicle class with a start() and stop() method. If we create a Car subclass that extends the Vehicle class, it should be able to be used in any part of the program that expects a Vehicle object and calls the start() and stop() methods.

The Interface Segregation Principle (ISP) states that clients should not be forced to implement interfaces they don’t use. This means that a class should not depend on methods that it does not use. This is achieved by creating multiple, smaller interfaces for different groups of methods, rather than one large interface that includes all of the methods. For example, consider a Document interface that has methods for loading, saving, and printing a document. If we have a class that only needs to load and save documents, it should not be forced to implement the print() method. Instead, we can create a separate `

--

--

Sampath Dissanayake
Sampath Dissanayake

No responses yet