Solid Principles: S - Single Responsibility
Solid Principles: S - Single Responsibility
S – Single Responsibility
0 – Open Closed Principle
L – Liskov substitution Principle
I – Interface Segregation Principle
D – Dependency Inversion Principle
Benefits:
Loose Coupling
Code Maintainability
Dependency Management
Single Responsibility: A class should have one and only one reason to change, means a class
should have only
- One can easily modify the above class and create bugs.
This can be achieved by interface
This way one does not modify the code in the existing class Add Operation.
Liskov Substiution:
Derived classes must be substitutable for their base classes.
Interface Segregation:
Make fine grained interfaces that are client specific.
Classes implementing the interface should not inherit unnecessary methods. Here square class
is implementing volume unnecessarily.
Fix:
Dependency Inversion:
High Level Modules should not depend on Low Level Modules. Both should depend on
abstractions. Here Desktop class is tightly coupled with monitor and keyboard.
Instead of passing generic keyboard qwerty we can have abstract as keyboard can be gaming
keyboard or any other key board. So create an interface keyboard.
Design Patterns:
Creational Pattern:
Factory Pattern:
Code Example: