Design Patterns Flashcards
(10 cards)
Strategy Pattern
Instead of hardcoding behavior, you define different strategies (algorithms) and switch them at runtime.
Example: Normal, Sporty, Off Road Vehicles
Observer Pattern
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.
Decorator Pattern
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.
Why cannot we use Static class instead of Singleton class?
- 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.
Types of Design Patterns?
- Creational Pattern
- Structural Pattern
- Behavioral Pattern
Creational Pattern
These deal with object creation mechanisms:
Singleton, Factory, Abstract, Builder, Prototype
Structural Pattern
These are concerned with object composition, i.e., how classes and objects are composed to form larger structures.
Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
Behavioral pattern
These focus on communication between objects, what goes on between objects and how they operate together:
Observer, Strategy, Command, Chain of Responsibility, State, Template Method, Iterator, Mediator, Visitor, Memento, Interpreter
Factory Pattern
Category: Creational
Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate.
Use Case: Object creation with conditional logic or varying types (e.g., notifications, shapes).
Key Benefit: Decouples client code from specific classes.
Example:
INotification n = NotificationFactory.CreateNotification(“Email”);
n.Notify(“Hello”);
Abstract Factory Pattern
Category: Creational
Intent: Provide an interface for creating families of related objects without specifying their concrete classes.
Use Case: UI toolkits across platforms (e.g., Windows vs. Mac buttons/checkboxes).
Key Benefit: Ensures compatibility between related products; enforces consistency.
Example:
IGUIFactory factory = new MacFactory();
factory.CreateButton().Render();
factory.CreateCheckbox().Render();