S - Single Responsibility Principle
“One class should have one and only one responsibility”
O - Open Closed Principle
“Software components should be open for extension, but closed for modification”
L - Liskov’s Substitution Principle
“Derived types must be completely substitutable for their base types”
I - Interface Segregation Principle
Make fine grained interfaces that are client specific.
Don't create fatty interface, Implementation classes require to implement unnecessary methods
D - Dependency Inversion Principle
Dependency Inversion principle states that:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.
1. DRY (Don't Repeat Yourself)
Avoid code duplication
2. Encapsulate what changes
Hides implementation details, It will help in maintenance
3. Favor Composition over Inheritance
Code re-usability without cost of inflexibility
4. Code to Interface
Helps in maintenance, improve flexibility
5. Delegation Principle
Do not do all the stuffs by yourself, delegate it.
6. KISS (Keep It Simple, Stupid)
Types of Design Patterns
-
Creational Patterns - Deals with the process of creation of objects of a class.
-
Structural Patterns - deals with objects and arrangement of objects (IS A, HAS A)
-
Behavioral Patterns - concerned with communication between objects.
-
J2EE Patterns - specifically concerned with the presentation tier
- Singleton - Single instance of class. Either Eager (static) or Lazy. Eg: Logger, Configurer
- Builder - Way to construct complex objects ( immutable )Eg: StringBuilder, PizzaMaker
- Prototype - Wrapper of clone method, Copying costly-creation objects.
- Factory - Create types of instances. Eg: Trade type (Bonds, Bill, Notes)
- Abstract Factory - Factory of factories of related products. Eg: Trade Type (GermanBond, EuroBond, GermanBill, EuroBill)
- Object pool - Re-use instances. Eg: Threadpool, objectpool
- Adapter - Adapting to a non-direct-compatible class/module/system
- Facade - Hiding complexity behind simple interfaces. Eg: Turbo Tax
- Decorator - Attaches additional functionality(enrichment) to objects at runtime. Eg: synchronizedXxx(), unmodifiableXxx()
- Proxy - Eg: Hibernate proxy for lazy fetch. @Spy instances for JUnit/Mockito.
- Flyweight - Store common characteristics of multiple objects at single place. Eg: String Interning
- Composite - Allows composing objects into a tree-like structure and treat it like a single object. Eg:tree, Folder and files
- Bridge - Decouple abstraction from implementation so that both can vary independently. Bridge pattern is often created using Adapter pattern.
- Observer - publish-subscribe pattern. Eg: Reactive streams.
- Chain of Responsibility - Object passes through various instances, coordinated by Handler. Eg: A try-catch block with multiple catch
- Visitor - Object which visits all similar instances. Eg: File Traverse, or Calculate total bill from cart.
- Command - Object encapsulates action with common interface which executor calls. Eg: Remote click.
- Strategy - Switch the algorithm/strategy at run time based upon the situation. Eg: Collections.sort() and Arrays.sort()
- State - Behavior based on current state of object.
- Iterator - Iterating through a collection of objects.
- Mediator
- Interpreter - Format converter. Eg: JVM converts byte to native for JVM.
- Memento - Snapshot. Save and restore state of object. Eg: UseCase - implement undo feature