The design principles are set of guidelines which help us to avoid bad design.A design is said to be bad if it has any one or all the features listed below:
- Rigid: Hard to change and not flexible.If we have to change part of code, it forces us to change many other parts of the implementation.
- Fragile: Easily broken or damaged.Easily breaks the functionality of other parts. When we change part of code it breaks implementation in other parts.
- Immobility: Not reusable.We cannot reuse the code in other parts as it is difficult to disentangle from the current implementation.
- Single Responsibility Principle
- Open Close Principle
- Liskov's Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
Here we discuss just the principles which will help us to make the design better. In practice we have to put some extra effort to adopt these principles in our code by using the most appropriate pattern which fits our requirement.
Single Responsibility Principle
In this context responsibility means a reason to change the class. This principle states that if we have two reasons to change the class then we have to split the class into two different classes. Each class will handle only one responsibility and if we need to change in functionality, only the class which implements the functionality will be changed.
“A class should have one reason to change”
If the class has more than one reason to change then we need to divide the class.
