Design Patterns

SOLID Principals

Solid principals are some best practices that can be followed in software development and also called Design Principal.

S: Single Responsibility Principal (SRP), SRP means that every object will have a single responsibility and area of focus. It is similar to separation of concerns (SoC). A class with multiple responsibilities makes it more complicated to change since it might affect more than one function at once. Meanwhile, a class with just one responsibility can be easily modified. This makes the class much more maintainable.

O: Open Closed Principal (OCP), OCP means that all the classes in the program, will be open for extensions but closed for modification. In simple terms, a class can be derived but can not be modified.

Assume your client approaches you to write the code for calculating the sum of numbers. You write a class (Add) with a method (Calculate) to calculate the sum and give it to the client, who is delighted with your work. Some time later, the same client approaches you to add the multiplication logic.
Now you edit the same class (Changing Add to Operation) and two methods (Add and Multiply) with the respective logic. However, this is not the right approach because your client may then ask you to add subtraction or division, causing you to modify the source code for each request.
As the principle states, for any modifications, a class should be allowed to extend its behavior but not to change its source code. Instead, it’s better write an interface (Operation Interface) with a calculate method, which the classes (Add or Multiply) can implement. When the client approaches you for another operation like division, you can create a new class by implementing the interface.

L: Liskov Substitution Principle (LSP), LSP means that user should use the derived class instead of its parent class without any functional change.
As per the object-oriented programming, when Class B extends Class A, Class B can be a substitute for Class A. LSP extends the same fundamental principle, with a significant change – Class B SHOULD be a substitute of Class A.

I: Interface Segregation Principal, ISP means that you should use multiple different interfaces rather than using a generic interface so they better satisfy the client needs.

D: Dependency Inversion Principal (DIP), DIP is about isolating your classes from concrete implementation and having abstract classes and interfaces. It increases the flexibility within a system by ensuring that you are not tightly coupled to one implementation.

Some Other Best Practice Rules:

KISS: Keep It Simple Stupid, Means make sure you keep things simple and don’t over complicate things. Don’t forget the purpose of this rule is to keep things simple and not easy.

DRY: Do not Repeat Yourself, You have to make sure that you don’t repeat the code anywhere, if a code is required in multiple places, put it some where in a method and use it.

YAGNI: You Ain’t Gonna Need It, never put things which are not required in your program.

SOC: Separation of Concerns, Every piece of code should have a single responsibility so that the program doesn’t get complicated.

Leave a Reply

Your email address will not be published. Required fields are marked *