Design Pattern
SOLID
Single Responsibility
There should not be more than one reason for a class to change. Class provides focused responsibility.
Questions -
What is this class supposed to do ? And what this class is doing?
Open Closed
Software Entities(Classes, Modules, Methods etc.) should be open for extension, but closed for modification.
We should be able to extend the existing behaviour.
Whatever is already written should not be change, we should not be modifying the code written in base class.
Liskov Substitution
We should be able to substitute base class objects with derived class object & this should not alter behaviour/characteristics of program.
Ex - Extending Rectangle to create Square.
Interface Segregation
Clients should not be forced to depend upon the interface that they do not use.
Interface Pollution -
No large interfaces,
Unrelated,
Classes have empty method implementations.
Method implementations throw UnsupportedOperationException(or similar)
Method implementations return null or default/dummy values.
To break to bigger interfaces so that the methods/ behaviours / contract defined in a particular interface are cohesive that are related to each other and we don’t run into a situation where a class is forced to provide implementation of a method for which it doesn’t make any sense.
Dependency Inversion
High level module should not depend on low level module. Both should depend upon abstractions.
Abstraction should not depend upon details. Details should depend upon abstraction.
Simple Factory
FactoryMethod
Difference from simple factory is you don’t need to make changes in switch case to add more implementations.
Code example -
Comments
Post a Comment