Solid Rule in Object-Oriented Programming

Solid Rule in Object-Oriented Programming

Solid Rule in Object-Oriented Programming

Introduction

SOLID is an acronym for the five main principles of Object-oriented programming.

  • S - Single Responsibility Principle.
  • O - Open-closed Principle.
  • L - Liskov Substitution Principle.
  • I - Interface Segregation Principle.
  • D - Dependency Inversion Principle.

Purpose of the rule

  • To write high-quality and maintainable code.
  • To design the system correctly.
  • To make the codebase readable to a newcomer to the project.
  • To avoid multilevel inheritance.

1. Single Responsibility Principle.

This is the most important rule of the SOLID principle. It encompasses experience, logic, and common sense.

The requires that a single class belongs to a single file and it only performs one task to answer a question. There must be only one reason to make changes to the class. Constant, enumerations and interfaces must be organized in the same way as the code.

Functions or Methods should only do one thing and must be testable. also one should pay attention to the line of code.

2. Open-closed Principle.

This rule is about inheritance. It requires that a class must be closed to modification and open to an extension so that new functionality can be added to an existing class through inheritance and not modifying the existing functionality.

Any changes should not affect the parent class when inherited.

3. Liskov Substitution Principle.

This rule also applies to inheritance. It explains how to properly override parent class methods.

It is also about implementing inheritance such that a subclass cannot contradict, or change the behavior or meaning of the parent class, if it overrides parent methods it must support parent logic and should not affect general results.

Liskov substitution and open-closed principle solve the problem of inheritance. If there is no inheritance then there is no problem observed by the two principles.

Another way to extend existing functionality is through dependency injection where functionality is extended without inheritance DI tries to avoid inheritance.

4. Interface Segregation Principle

Interfaces are used to provide security to the program by hiding the function's implementation. This principle state that you cannot override a class and force it to implement and execute functionality that it does not need.

5. Dependency Inversion Principle

Is a principle based on dependency injection? The class should depend on one abstraction not on the specific implementation of those abstractions.

The purpose of this principle is to weaken class relationships and combat the complexity of the system

This principle is explained in detail with the clean architecture design principle.

Follow through this article Clean Architecure