Design Patterns
Reusable solutions to common design problems, grouped into creational, structural and behavioural patterns.
Cheat sheet
- ▸Creational: Singleton, Factory, Abstract Factory, Builder, Prototype.
- ▸Structural: Adapter, Decorator, Facade, Proxy, Composite.
- ▸Behavioural: Observer, Strategy, Command, Iterator, State, Template Method.
- ▸Singleton: private constructor + static instance (thread-safe via double-checked locking or enum).
- ▸Observer: subject notifies subscribers (event listeners, pub/sub).
- ▸Builder: step-by-step construction of complex objects.
Pattern → real example
| Pattern | Category | Real example |
|---|---|---|
| Singleton | Creational | Logger, config |
| Factory | Creational | Payment method creator |
| Adapter | Structural | Charger adapter / legacy API wrapper |
| Decorator | Structural | Java I/O streams |
| Observer | Behavioural | YouTube subscriptions |
| Strategy | Behavioural | Sorting / discount rules |
Visual
class Logger {
private static volatile Logger inst;
private Logger() {}
static Logger get() {
if (inst == null) synchronized(Logger.class) {
if (inst == null) inst = new Logger(); }
return inst; } }Top 10 campus interview questions
Quick quiz
1. Ensuring only one instance exists:
2. Java BufferedReader wrapping FileReader is:
3. Pub/sub notification follows:
4. Observer is a ___ pattern:
5. Avoiding constructors with many params: