Design Patterns Flashcards
1
Q
Strategy Pattern
A
Instead of hardcoding behavior, you define different strategies (algorithms) and switch them at runtime.
Example: Normal, Sporty, Off Road Vehicles
1
Q
Observer Pattern
A
When the state of one object (the subject) changes, all its observers (or subscribers) are notified and updated automatically.
Example: Amazon - Notify when the product is back in stock.
2
Q
Decorator Pattern
A
It allows behavior to be added to individual objects, dynamically and transparently, without modifying the class itself.
Example - Pizza and toppings, Coffee and additional toppings.
3
Q
Why cannot we use Static class instead of Singleton class?
A
- Static class is not initialized while Singleton class is initialized only if it is required.
- Static class cannot be passed as a parameter, which is widely used with Singleton.
By dependency injection we keep it loosely coupled.