Open In App

SOLID Principles in Programming: Understand With Real Life Examples

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In software development, Object-Oriented Design plays a crucial role when it comes to writing flexible, scalable, maintainable, and reusable code. There are so many benefits of using OOD but every developer should also know the SOLID principle for good object-oriented design in programming. The SOLID principle was introduced by Robert C. Martin, also known as Uncle Bob and it is a coding standard in programming. This principle is an acronym of the five principles which are given below:

  • Single Responsibility Principle (SRP)
  • Open/Closed Principle
  • Liskov’s Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)

SOLID-Principle-in-Programming-Understand-With-Real-Life-Examples

The SOLID principle helps in reducing tight coupling. Tight coupling means a group of classes are highly dependent on one another which you should avoid in your code.

  • Opposite of tight coupling is loose coupling and your code is considered as a good code when it has loosely-coupled classes.
  • Loosely coupled classes minimize changes in your code, helps in making code more reusable, maintainable, flexible and stable. Now let’s discuss one by one these principles…

1. Single Responsibility Principle

This principle states that “A class should have only one reason to change” which means every class should have a single responsibility or single job or single purpose. In other words, a class should have only one job or purpose within the software system.

Let’s understand Single Responsibility Principle using an example:

Imagine a baker who is responsible for baking bread. The baker’s role is to focus on the task of baking bread, ensuring that the bread is of high quality, properly baked, and meets the bakery’s standards.

  • However, if the baker is also responsible for managing the inventory, ordering supplies, serving customers, and cleaning the bakery, this would violate the SRP.
  • Each of these tasks represents a separate responsibility, and by combining them, the baker’s focus and effectiveness in baking bread could be compromised.
  • To adhere to the SRP, the bakery could assign different roles to different individuals or teams. For example, there could be a separate person or team responsible for managing the inventory, another for ordering supplies, another for serving customers, and another for cleaning the bakery.

2. Open/Closed Principle

This principle states that “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” which means you should be able to extend a class behavior, without modifying it.

Let’s understand Open/Closed Principle using an example:

Imagine you have a class called PaymentProcessor that processes payments for an online store. Initially, the PaymentProcessor class only supports processing payments using credit cards. However, you want to extend its functionality to also support processing payments using PayPal.

Instead of modifying the existing PaymentProcessor class to add PayPal support, you can create a new class called PayPalPaymentProcessor that extends the PaymentProcessor class. This way, the PaymentProcessor class remains closed for modification but open for extension, adhering to the Open-Closed Principle

3. Liskov’s Substitution Principle

The principle was introduced by Barbara Liskov in 1987 and according to this principle “Derived or child classes must be substitutable for their base or parent classes“. This principle ensures that any class that is the child of a parent class should be usable in place of its parent without any unexpected behavior.

Let’s understand Liskov’s Substitution Principle using an example:

One of the classic examples of this principle is a rectangle having four sides. A rectangle’s height can be any value and width can be any value. A square is a rectangle with equal width and height. So we can say that we can extend the properties of the rectangle class into square class.

In order to do that you need to swap the child (square) class with parent (rectangle) class to fit the definition of a square having four equal sides but a derived class does not affect the behavior of the parent class so if you will do that it will violate the Liskov Substitution Principle.

4. Interface Segregation Principle

This principle is the first principle that applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. It states that “do not force any client to implement an interface which is irrelevant to them“. Here your main goal is to focus on avoiding fat interface and give preference to many small client-specific interfaces. You should prefer many client interfaces rather than one general interface and each interface should have a specific responsibility.

Let’s understand Interface Segregation Principle using an example:

Suppose if you enter a restaurant and you are pure vegetarian. The waiter in that restaurant gave you the menu card which includes vegetarian items, non-vegetarian items, drinks, and sweets.

  • In this case, as a customer, you should have a menu card which includes only vegetarian items, not everything which you don’t eat in your food. Here the menu should be different for different types of customers.
  • The common or general menu card for everyone can be divided into multiple cards instead of just one. Using this principle helps in reducing the side effects and frequency of required changes.

5. Dependency Inversion Principle

The Dependency Inversion Principle (DIP) is a principle in object-oriented design that states that “High-level modules should not depend on low-level modules. Both should depend on abstractions“. Additionally, abstractions should not depend on details. Details should depend on abstractions.

  • In simpler terms, the DIP suggests that classes should rely on abstractions (e.g., interfaces or abstract classes) rather than concrete implementations.
  • This allows for more flexible and decoupled code, making it easier to change implementations without affecting other parts of the codebase.

Let’s understand Dependency Inversion Principle using an example:

In a software development team, developers depend on an abstract version control system (e.g., Git) to manage and track changes to the codebase. They don’t depend on specific details of how Git works internally.

This allows developers to focus on writing code without needing to understand the intricacies of version control implementation.



Similar Reads

Design Patterns: Understand The Importance With Real Life Examples
Software Design Patterns... Software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. This is the definition written on Wikipedia for software design patterns...The above definition makes sense to you if you are an experienced developer and spent some time writing the code for
8 min read
DynamoDB: Understand The Benefits With Real Life Use Cases
Building a web application is always exciting for a newbie software engineer. As a web developer, you might have started with building the frontend then backend and then you add features one by one. You choose some database for your application to store the information. Now your web application starts growing and you start getting the users for tha
8 min read
Leadership vs Management: Understand Key Differences
Leadership and Management are wholly different things leadership is not management, and vice versa. Management dates back to Henry Ford, and scientific management to Frederick Taylor. Right now, our education system is giving more importance to management education while downplaying the value of leadership. Also, there is a perspective change that
9 min read
Dependency Inversion Principle (SOLID)
Let's understand one of the key principles of SOLID principles group namely, Dependency inversion principle. Dependency inversion principle is one of the principles on which most of the design patterns are build upon. Dependency inversion talks about the coupling between the different classes or modules. It focuses on the approach where the higher
3 min read
Single Responsibility in SOLID Design Principle
SOLID principle is an acronym that stands for 5 key principles in software development: S - Single Responsibility Principle O - Open Closed Principle L - Liskov Substitution PrincipleI - Interface Segregation PrincipleD - Dependency Inversion Principle It has become one of the key principles in modern software development to ensure the code is usin
4 min read
What is the Difference between Solid and Dotted Line in Sequence Diagram?
In Sequence Diagrams, solid and dotted lines are used to show different kinds of messages between objects. A solid line represents a message where the sender waits for a response from the receiver before continuing.A dotted line represents a message where the sender does not wait for a response and continues right awaySolid Line A solid line repres
2 min read
System Development Life Cycle vs. System Design Life Cycle
The System Development Life Cycle (SDLC) and System Design Life Cycle (SDLC) are two distinct but interconnected processes involved in the development of information systems. Table of Content System Development Life Cycle (SDLC)System Design Life Cycle (SDLC)System Development Life Cycle vs System Design Life CycleLet's explore each life cycle and
4 min read
7 Common Programming Principles That Every Developer Must Follow
If we gave you half a story to continue it, how would you do that? How would you proceed further and add your storyline there? Firstly, you need to understand the complete story, you will search for all the characters, their role in different chapters or parts of the story, which characters you need to take till the end or which one has the role on
7 min read
Top Heuristic Principles with Examples For Interactive Designs
Only designing interfaces is not the end, as a designer, you must have a solid understanding of how to measure it for your own designs. How do you determine whether your goods are genuinely usable? Once the designs are ready, to run a usability test heuristics principles come into the picture, using which we carry out the heuristic evaluation. Thou
13 min read
Principles of Software Design
Prerequisites - Software design process Software design process | Set 2 Modern principles of software development Design means to draw or plan something to show the look, functions and working of it. Software Design is also a process to plan or convert the software requirements into a step that are needed to be carried out to develop a software sys
3 min read