Design Patterns – Singleton, Observer, Factory, Adapter, Iterator, Strategy, Decorator
More Design Patterns articles:
- Design Patterns Fundamentals (GoF)
- Creational Patterns (Singleton, Factory, Builder, Prototype, Abstract Factory)
- Design Patterns Catalog
This article explains key Design Patterns—including exam questions and reference tags.
In a Nutshell
Design Patterns are proven solutions to recurring problems in software development. They provide standardized templates for common scenarios and improve code quality, maintainability, and team communication.
Technical Overview
The “Gang of Four” (GoF) popularized Design Patterns as tested strategies for typical object-oriented programming challenges. They divide into three categories: Creational Patterns (Singleton, Factory), Structural Patterns (Adapter, Decorator), and Behavioral Patterns (Observer, Strategy, Iterator). Patterns promote loose coupling, high cohesion, and reusability. Rather than ready-made algorithms, they’re abstract solution descriptions that must be adapted to specific contexts.
Key Exam Topics
- Creational Patterns: Singleton, Factory, Abstract Factory, Builder
- Structural Patterns: Adapter, Decorator, Facade, Proxy, Composite
- Behavioral Patterns: Observer, Strategy, Iterator, Command, Template Method
- Singleton: Global instance, thread-safe implementation
- Observer: Publisher-Subscriber, loose coupling between objects
- Factory: Object creation without knowing concrete classes
- Adapter: Interface compatibility between incompatible classes
- Iterator: Sequential access to collection elements without exposing internal structure
- Strategy: Interchangeable algorithms via interface-based implementation
Core Components
- Singleton Pattern (global instance)
- Factory Pattern (object creation)
- Observer Pattern (observer mechanism)
- Adapter Pattern (interface compatibility)
- Iterator Pattern (sequential access)
- Strategy Pattern (algorithm switching)
- Decorator Pattern (dynamic extension)
- Template Method Pattern (algorithm skeleton)
- MVC Pattern (architectural pattern)
- Registry Pattern (centralized registration)
Practical Example
// Singleton Pattern Example
public class DatabaseConnection {
private static DatabaseConnection instance;
private DatabaseConnection() {
// Private Constructor
}
public static synchronized DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
}
// Observer Pattern Example
interface Observer {
void update(String message);
}
class EmailNotification implements Observer {
public void update(String message) {
sendEmail(message);
}
}
Explanation: Singleton ensures a global instance, while Observer enables notification when state changes occur.
Advantages and Disadvantages
Advantages
- Proven solutions for known problems
- Better communication between developers
- Promote loose coupling and high cohesion
- Increase reusability and maintainability
Disadvantages
- Can introduce unnecessary complexity
- Learning curve for beginners
- Sometimes over-engineering simple problems
- Require context-appropriate adaptation
Common Exam Questions (with Quick Answers)
-
Two main categories of Design Patterns? Creational (creation), Structural (structure), Behavioral (behavior).
-
Purpose of the Singleton Pattern? Ensure exactly one instance of a class exists and is globally accessible.
-
When to use the Observer Pattern? When multiple objects need to be notified of state changes in another object.
-
What problem does Factory Pattern solve? Object creation without the client code needing to know concrete classes.
-
Adapter vs. Decorator? Adapter adjusts interfaces; Decorator adds functionality dynamically.
-
Iterator Pattern advantage? Provides uniform access to different collection types without revealing internal structure.
-
When to use Strategy Pattern? When algorithms need to be interchangeable without changing client code.
-
MVC Pattern components? Model (data), View (presentation), Controller (logic connecting Model and View).
Key Resources
- https://refactoring.guru/design-patterns
- https://de.wikipedia.org/wiki/Entwurfsmuster
- https://www.oracle.com/technical-resources/articles/j2ee/patterns
Recommended Reading: Design Patterns
Design Patterns
Books about design patterns and software design
Design Patterns von Gang of Four
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
Patterns of Enterprise Application Architecture von Martin Fowler
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
Refactoring von Martin Fowler
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.





