Saturday 4 February 2023

SOLID principle

 

The SOLID principles are a set of five design principles that are aimed at helping software developers create maintainable, scalable, and flexible software systems. The

SOLID principles are:

Single Responsibility Principle (SRP): A class should have only one reason to change, meaning that a class should have only one responsibility.

Example: A class that represents a bank account should only be responsible for performing operations related to the bank account such as deposit, withdrawal, and balance inquiry, and not be responsible for printing statements or sending notifications to the account owner.

Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification, meaning that existing code should not be modified when adding new functionality.

Example: A class that represents a shape can be designed to be open for extension (i.e., adding new shapes), but closed for modification (i.e., modifying existing code). The class can be designed with an abstract shape class and concrete classes for each type of shape, and adding new shapes would not require modifying existing code.

Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types, meaning that objects of a derived class should be able to replace objects of the base class without affecting the correctness of the program.

Example: A class that represents a rectangle should be a subtype of a class that represents a shape. This means that any code that works with a shape should work with a rectangle without any modification, as a rectangle is a valid substitute for a shape.

Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use, meaning that each interface should have a specific and well-defined purpose.

Example: A class that represents an animal should not be forced to implement an interface that defines methods for flying and swimming if the animal cannot fly or swim. Instead, separate interfaces can be created for flying and swimming animals and implemented only by those classes that can perform those actions.

Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules. Both should depend on abstractions, meaning that the design should strive to reduce coupling between components and make them more modular.

Example: A class that represents a high-level policy should not depend on a low-level implementation detail. Instead, both the high-level and low-level classes should depend on an abstraction, such as an interface, that defines the contract between the two. This allows the high-level class to be more flexible and less prone to breaking if the low-level implementation changes.

Adhering to the SOLID principles can help us create systems that are easier to maintain, test, and extend, and can also make it easier to identify and correct problems when they arise.

No comments:

Post a Comment