Design Patterns Summary
Singleton Pattern
Ensures a class has only one instance and provides a global point of access to it.
Use Cases: Logger, Configuration Manager, Database Connection.
Advantages: Controlled instance, efficient memory use.
Disadvantages: Global state can hurt testability, not thread-safe by default.
Factory Pattern
Provides an interface for creating objects without specifying the exact class.
Use Cases: GUI libraries, Shape or Button factory.
Advantages: Promotes loose coupling, easy to introduce new types.
Disadvantages: Complexity increases with more classes.
Abstract Factory Pattern
Produces families of related or dependent objects without specifying their concrete classes.
Use Cases: UI themes (dark/light), cross-platform support.
Advantages: Consistent interface, isolates concrete classes.
Disadvantages: Hard to extend with new product families.
Builder Pattern
Separates the construction of a complex object from its representation.
Use Cases: Creating complex objects like documents, meals.
Advantages: Step-by-step construction, reusable builders.
Disadvantages: Increased complexity in code structure.
Prototype Pattern
Creates new objects by copying an existing object (prototype).
Use Cases: Game object cloning, performance-sensitive object creation.
Advantages: Avoids cost of new object creation.
Disadvantages: Cloning complex objects can be tricky.
Adapter Pattern
Allows incompatible interfaces to work together by acting as a bridge.
Use Cases: Legacy code integration, API adaptation.
Advantages: Increases reusability and flexibility.
Disadvantages: Can increase complexity of code.
Decorator Pattern
Adds responsibilities to objects dynamically without modifying their structure.
Use Cases: UI elements, input/output stream enhancement.
Advantages: More flexible than subclassing.
Disadvantages: Can result in many small classes.
Observer Pattern
Defines a one-to-many dependency so that when one object changes state, others are
notified.
Use Cases: Event systems, GUIs, real-time systems.
Advantages: Promotes loose coupling.
Disadvantages: May lead to memory leaks if observers are not deregistered.
Strategy Pattern
Defines a family of algorithms and makes them interchangeable at runtime.
Use Cases: Sorting algorithms, payment strategies.
Advantages: Improves flexibility and reusability.
Disadvantages: Increases the number of classes.
Command Pattern
Encapsulates a request as an object, allowing parameterization and queuing.
Use Cases: GUI buttons, transactional behavior.
Advantages: Decouples sender and receiver.
Disadvantages: Can result in lots of command classes.