S.O.L.I.D Principles

solid principles

The SOLID Principles

S — Single Responsibility

A class should have a single responsibility If a Class has many responsibilities, it increases the possibility of bugs because making changes to one of its responsibilities, could affect the other ones without you knowing.

Goal

This principle aims to separate behaviours so that if bugs arise as a result of your change, it won’t affect other unrelated behaviours.

O — Open-Closed

Classes should be open for extension, but closed for modification Changing the current behaviour of a Class will affect all the systems using that Class. If you want the Class to perform more functions, the ideal approach is to add to the functions that already exist NOT change them.

Goal

This principle aims to extend a Class’s behaviour without changing the existing behaviour of that Class. This is to avoid causing bugs wherever the Class is being used.

L — Liskov Substitution

It is necessary that subclass should be changeable for its superclass

Goal

This principle aims to enforce consistency so that the parent Class or its child Class can be used in the same way without any errors.

I — Interface Segregation

Clients should not be forced to depend on methods that they do not use. When a Class is required to perform actions that are not useful, it is wasteful and may produce unexpected bugs if the Class does not have the ability to perform those actions. A Class should perform only actions that are needed to fulfil its role. Any other action should be removed completely or moved somewhere else if it might be used by another Class in the future.

Goal

This principle aims at splitting a set of actions into smaller sets so that a Class executes ONLY the set of actions it requires.

D — Dependency Inversion

– High-level modules should not depend on low-level modules. Both should depend on the abstraction.

– Abstractions should not depend on details. Details should depend on abstractions.

Goal

This principle aims at reducing the dependency of a high-level Class on the low-level Class by introducing an interface.