S.O.L.I.D. Class Design Principles

Abbreviation
Full Form
Definition
Reason
S
Single Responsibility Principle A class should have only one reason to change i.e. should have only one purpose to serve.  - loosely coupled
- less change lead to less break down of application
- helps to change identity of object without affecting other modules

O
Open-Closed Principle A class should be open for extension closed for modification. - Anyone need to make change in your class has to inherit the class and change. This allows the existing functionality to stay intact.
L
Liskov Substitution Principle Derived types should always be substitutable by its parent class (base type). Derived types should be compatible with its base type.
For e.g.: Square is a Rectangle i.e. Square represents a Rectangle.
In case, if we try to set dimension of a Rectangle reference pointing to Square instance, the flow will be weird as it will always override the dimension of rectangle.
I
Interface Segregation Principle Client should not be forced by interface to implement unnecessary methods that it do not require. It is closely related to first principle. An interface should have only one purpose. If it provides more than one method and a derived class wants to implement only one method, it has no choice but to implement the other method and keep it empty! It gives the user the flexibility to add required functionality only.
D
Dependency Inversion Principle Depend on abstraction rather than concrete implementation. If the class depend on one another they are tightly coupled as change in one will impact other. Hence, classes must be separated from each other and bonded via abstract layer. This provides compatibility between a classes and derived types. 

Comments

Popular posts from this blog

@Overrride annotation introduced in Java1.5

Liskov Substitution Principle (LSP)

Interface Segregation Principle