Design Patterns Quiz
Singleton, Observer, Factory & co. — the Gang-of-Four classics with small code examples.
- Singleton
- A class has only one single instance in the entire program
- Singleton: global access + max 1 instance. Typical uses: logger, config, DB connection. Anti-pattern suspicion: often abused as global state.
- Observer
- Multiple objects should be notified automatically when a state changes
- Subject holds a list of observers, notifies them on state change. Examples: event listeners (DOM), RxJS, Vue/React reactivity, MQTT.
- Factory
- Creates objects without the client needing to know the concrete class
- createButton(theme) → returns WindowsButton or MacButton. Client only knows the interface. Also known: Abstract Factory (families of related objects).
- Pattern → Category
- Zuordnen
- Creational = object creation, Structural = composition of classes/objects, Behavioral = distribution of responsibility & communication.
- Pattern → Use case
- Zuordnen
- Facade vs Adapter: Facade simplifies (1→1, simpler interface), Adapter bridges incompatible (translates). Both are Structural.
- Adapter
- A wrapper class that translates one library's methods into the interface the rest of the code expects
- Classic: old XML library, new JSON service client → Adapter translates. Real world: EU → US power adapter. Strategy tip: keep all library-touching code thin behind adapters.
- Decorator vs Adapter
- Decorator KEEPS the interface and ADDS functionality — Adapter CHANGES the interface
- Decorator: same interface, more behavior. Adapter: different interface, same behavior. Example Decorator: ScrollDecorator(WindowDecorator(BorderDecorator(View))) — Java IO works this way.
- Dependency Injection
- Objects receive their dependencies from outside (constructor / setter / method) instead of creating them themselves
- Spring, Angular, NestJS are built on this. Benefit: testability (mocks possible), decoupling. Anti-pattern: Service Locator (hides the dependency).
- Facade
- jQuery $() hides complex DOM operations behind a simple interface
- Facade = simplified interface to a complex subsystem. jQuery, axios, fetch (for XHR), AWS SDK — all are facades.
- SOLID — Order
- , ,
- SOLID — coined by Robert C. Martin ("Uncle Bob"). Acronym by Michael Feathers. Critique: very Java/enterprise-focused, less relevant in functional languages.
- SOLID: "S"
- Single Responsibility — a class should have only one reason to change
- One class / one module = one clearly bounded responsibility. Signs of violation: many imports, long class, frequent changes for different reasons.
- SOLID: "D"
- Flip dependencies so high-level modules don't depend on low-level modules — both depend on abstractions
- Instead of OrderService → MySqlRepo (concrete), it becomes OrderService → IRepository ← MySqlRepo. Enables plug-in architecture and testability.
- SOLID principle → meaning
- Zuordnen
- Liskov is named after Barbara Liskov (MIT, Turing Award). Violation: rectangle-square problem (square as subtype of rectangle breaks setWidth/setHeight contracts).
- Command Pattern
- Actions are encapsulated as objects — they can be logged, queued, undone or sent over the network
- execute() / undo() / redo(). Classics: text editors, job queues (e.g. BullMQ), Redux actions are commands at heart.
- Iterator
- A uniform interface to traverse elements of a collection sequentially without knowing its internal structure
- In JS: Symbol.iterator + next(). In Python: __iter__ / __next__. Enables for...of over anything iterator-conforming — including generators.
- Pattern → Category
- Drag & Drop
- Memento stores state for undo. Composite: tree structure (leaves + composites with the same interface) — e.g. DOM. Chain of Responsibility: logging pipelines, Express middleware.
- Strategy vs State
- Strategy swaps algorithms from outside, State changes behavior based on internal state
- Strategy: "Which sorting algorithm shall we use?" — client decides. State: "What does the vending machine do in state `HasCoin`?" — object decides, transitions itself.
- MVC
- Model — View — Controller
- Model = data/logic, View = UI, Controller = mediator. Variants: MVP (Presenter), MVVM (ViewModel — Angular, Vue, WPF), MVI (Intent — Android). Separates UI from logic.
- Anti-pattern → symptom
- Zuordnen
- Magic numbers are fixed with named constants (SECONDS_PER_DAY = 86400). God Class via SRP refactoring. Tight coupling is reduced with DI and interfaces.
- Repository Pattern
- Data access — business logic uses repository.find() without knowing SQL/NoSQL/HTTP
- Separates domain logic from persistence. Example: UserRepository.findByEmail() — regardless of MySQL, MongoDB or REST behind. Important combo with DI.
- Pub/Sub vs Observer
- In Pub/Sub a broker mediates between publisher and subscriber — they don't know each other directly
- Observer: subject keeps direct list of observers. Pub/Sub: channel/topic-based, sender and receiver decoupled by broker (e.g. Redis Pub/Sub, RabbitMQ, MQTT).