articles

home / developersection / articles / solid principle

SOLID Principle

SOLID Principle

ICSM Computer 964 08-Jan-2025

The SOLID principles are a set of five design principles in object-oriented programming.

 

SOLID Principle

 

1. Single Responsibility Principle (SRP)

Definition: A class should have only one reason to change, meaning it should have only one responsibility.
Purpose: Avoids tightly coupling unrelated functionalities, making code easier to understand and maintain.
Example:
Instead of having the ReportManager class that both generates and sends reports, separate it into:

  • ReportGenerator for generating reports.
  • ReportSender for sending them.

 

2. Open/Closed Principle (OCP)

Definition: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
Purpose: Encourages writing code that can be extended without altering existing code, minimizing the risk of introducing bugs.
Example:
Use inheritance or interfaces to extend functionality rather than modify existing classes.

public interface IShape { double CalculateArea(); } public class Circle : IShape { public double Radius { get; set; } public double CalculateArea() => Math.PI * Radius * Radius; } public class Rectangle : IShape { public double Width { get; set; } public double Height { get; set; } public double CalculateArea() => Width * Height; } 

 

3. Liskov Substitution Principle (LSP)

Definition: Objects of a superclass should be replaceable with objects of its subclass without altering the correctness of the program.
Purpose: Ensures a subclass can stand for its superclass without unexpected behavior.
Example:
If Bird has a method Fly(), a subclass like Penguin that can't fly violates LSP.
To fix this, refactor the hierarchy:

public interface IBird { void Eat(); } public interface IFlyingBird : IBird { void Fly(); } public class Sparrow : IFlyingBird { public void Eat() { /* Implementation */ } public void Fly() { /* Implementation */ } } public class Penguin : IBird { public void Eat() { /* Implementation */ } } 

 

4. Interface Segregation Principle (ISP)

Definition: A class should not be forced to implement interfaces it does not use.
Purpose: Avoids creating large interfaces; promotes small, specific ones.
Example:

 

Instead of a single large interface:

public interface IVehicle { void Drive(); void Fly(); } 

Create smaller interfaces:

public interface IDrivable { void Drive(); } public interface IFlyable { void Fly(); } public class Car : IDrivable { public void Drive() { /* Implementation */ } } public class Airplane : IFlyable { public void Fly() { /* Implementation */ } } 

5. Dependency Inversion Principle (DIP)

Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.
Purpose: Makes systems flexible and reduces coupling between components.
Example:
Instead of directly instantiating dependencies:

public class DataAccess { public void GetData() { /* Implementation */ } } public class BusinessLogic { private DataAccess _dataAccess = new DataAccess(); } 

Use dependency injection with interfaces:

public interface IDataAccess { void GetData(); } public class DataAccess : IDataAccess { public void GetData() { /* Implementation */ } } public class BusinessLogic { private readonly IDataAccess _dataAccess; public BusinessLogic(IDataAccess dataAccess) { _dataAccess = dataAccess; } } 

Summary

The SOLID principles provide a framework for writing clean, maintainable, and scalable object-oriented code. Following these principles reduces coupling, enhances flexibility, and makes your codebase easier to understand and modify.

 


Updated 16-Jan-2025
ICSM Computer

IT-Hardware & Networking

Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.

Leave Comment

Comments

Liked By