In this lesson, we will learn the following.

  1. SOLID Acronym and Introduction
  2. SOLID Design Principles
  3. Why do we need to adapt SOLID

Let’s now discuss SOLID principles in detail.

Acronym

SOLID Acronym Short Description
S SRP Single Responsibility Principles
O OCP Open Closed Principles
L LSP LISKOV substitution principles
I ISP Interface Segregation Principles
D DIP Dependency Inverse Principles


Introduction

The motivation for using SOLID

Single Responsibility Principles

Uncle Bob expresses the principle as “A class should have only one reason to change”. Everything in a class should be related to a single purpose. There can be many members in a class as long as they are related to a single responsibility. With the help of SRP, classes become smaller and cleaner.

Some examples of responsibilities to consider that may need to be separated include.

Interface Segregation Principle

This principle was first used and formulated by Robert C. Martin (Uncle Bob) while consulting for Xerox.

We will take a case study that Uncle Box resolved for Xerox.

Problem

The design problem was that a single job class was used for almost all of the tasks.

Solution

One large job class is segregated into multiple interfaces depending on the requirements.

Actually, if you add methods that shouldn’t be there, the classes implementing the interface will have to implement those methods as well. That is why; the client shouldn’t be forced to depend on interfaces that they don’t use. ISP is intended to keep a system decoupled and thus easier to refactor, change, and deploy. Refer to the .NET code snippet attached to this tutorial.

If you look at the previous section, you can see that the interface has been segregated based on the requirements.

Open Closed Principles

This section covers.

  1. Open closed principle
  2. Implementation guidelines
  3. Example

Open Closed principle

Implementation Guidelines

Why OCP?

If not followed.

Example

Refer to the .NET code snippet attached, an Employee class calculating bonus based on the employee types (Permanent and Contract)

Liskov Substitution Principle

This section covers.

  1. Liskov Substitution principle
  2. Implementation guidelines
  3. Example

Liskov Substitution principle

Implementation Guidelines

It may be confusing, but this can be achieved through a simple example. Please refer to the attached .NET sample (LSP folder).

Dependency Inversion Principle

Here, let us cover.

Dependency Inversion Principle

Intention of usage

If you look at the traditional layered approach-

Presentation Layer => Business Layer => Data Access Layer

Here, the high-level module depends on the low-level module.

In order to resolve this coupling issue, we will be introducing an interface layer through which different layers can interact.

Business Layer => Interface Repository => This will be implemented in the Data Access Layer

The business layer would be communicating with the Data Access Layer via the interface.

This approach will help us to test each module independently.

Refer to the .NET Code attachment (DIP folder)

An adapter design pattern is one of the examples of DIP usage. We are not going to discuss the adapter design pattern in this article.