π¦ Creational Patterns β How objects are created
| Pattern | Intent | Participants | Key Points |
|---|---|---|---|
| Singleton | Ensure only one instance exists globally | Singleton |
Lazy init, thread-safe, global access |
| Factory Method | Create object without exposing instantiation logic |
Creator, Product, ConcreteCreator
|
Subclass decides which class to instantiate |
| Abstract Factory | Create families of related objects without specifying classes |
AbstractFactory, ConcreteFactory, Product
|
Ensures consistency among related products |
| Builder | Construct complex object step by step |
Builder, Director, Product
|
Useful for objects with many optional parts |
| Prototype | Clone existing object instead of creating new |
Prototype, Client
|
Requires deep copy/shallow copy logic |
βοΈ Structural Patterns β How objects are composed
| Pattern | Intent | Participants | Key Points |
|---|---|---|---|
| Adapter | Convert interface of a class into another expected by the client |
Target, Adapter, Adaptee
|
Wrapper around legacy code |
| Bridge | Decouple abstraction from implementation |
Abstraction, Implementor
|
Prefer over inheritance when abstraction and implementation vary |
| Composite | Treat individual and compositions uniformly |
Component, Leaf, Composite
|
Hierarchical tree structures |
| Decorator | Add responsibilities dynamically at runtime |
Component, ConcreteComponent, Decorator
|
Chain of wrappers |
| Facade | Provide simplified interface to complex subsystem |
Facade, Subsystem classes
|
High-level API to hide complexity |
| Flyweight | Share common state to reduce memory |
Flyweight, FlyweightFactory
|
Ideal for large number of similar objects |
| Proxy | Provide a placeholder or surrogate |
Subject, Proxy, RealSubject
|
Control access, lazy loading, logging |
π Behavioral Patterns β How objects interact
| Pattern | Intent | Participants | Key Points |
|---|---|---|---|
| Chain of Responsibility | Pass request along chain until handled |
Handler, ConcreteHandler
|
Avoids coupling sender to receiver |
| Command | Encapsulate a request as an object |
Command, Receiver, Invoker, Client
|
Supports undo/redo, logging |
| Interpreter | Define grammar and evaluate language syntax |
AbstractExpression, Terminal, NonTerminal
|
For domain-specific languages |
| Iterator | Sequential access to elements without exposing structure |
Iterator, Aggregate
|
Used with collections |
| Mediator | Centralize complex communications among objects |
Mediator, Colleague
|
Reduces coupling between components |
| Memento | Capture and restore object's state |
Originator, Memento, Caretaker
|
Used for undo operations |
| Observer | Notify dependent objects of state changes |
Subject, Observer
|
Push vs Pull, often used in event systems |
| State | Change behavior when internal state changes |
Context, State, ConcreteState
|
State transitions handled via delegation |
| Strategy | Define family of algorithms, make them interchangeable |
Strategy, Context
|
Replaces conditional logic |
| Template Method | Define skeleton of algorithm with steps defined in subclasses |
AbstractClass, ConcreteClass
|
Enforces algorithm structure |
| Visitor | Separate operations from object structure |
Visitor, Element, ConcreteVisitor
|
Adds new operations without changing classes |
β Quick Usage Examples
// Singleton
public class Logger {
private static Logger instance;
private Logger() {}
public static synchronized Logger getInstance() {
if (instance == null) instance = new Logger();
return instance;
}
}
// Strategy
interface PaymentStrategy { void pay(int amount); }
class CreditCardPayment implements PaymentStrategy { public void pay(int amount) {...} }
class PaymentContext {
private PaymentStrategy strategy;
public void setStrategy(PaymentStrategy s) { strategy = s; }
public void execute(int amount) { strategy.pay(amount); }
}
π Usage Tips
- π§© Favor composition over inheritance β especially with Strategy, Decorator, and State.
- π Use patterns together β e.g., Factory + Singleton, or Command + Memento.
- π Refactor to patterns β identify repetitive/rigid logic and abstract with patterns.
- π§ Don't over-engineer β apply patterns when solving concrete problems.
Top comments (0)