Design Patterns Theory
Design Patterns Theory
Singleton Pattern
Intent: Ensure a class has only one instance and provides a global access point to it.
Motivation: Avoid issues like resource overuse or incorrect behavior caused by multiple
instances, e.g., for configuration or logging.
Applicability: Use when only one instance of a class is required, and it must be accessible
globally.
Structure: A class maintains a static variable to hold its sole instance, a private constructor to
prevent direct instantiation, and a public static method to provide access to the instance.
Implementation: The class ensures thread safety (if needed) and lazy initialization for creating
the instance only when required.
Benefits:
Drawbacks:
Example: A logger class provides a global access point for logging across the application
without multiple instances.
Intent: Provide an interface for creating families of related objects without specifying their
concrete implementations.
Motivation: Useful for systems that need to work with multiple product families (e.g., GUI
components for Windows or Mac) while ensuring product consistency.
Applicability: Use when a system should be independent of how its products are created and
ensure compatibility between related products.
Structure: An abstract factory declares interfaces for creating various products. Concrete
factories implement these interfaces to produce specific products for a family.
Implementation: The pattern involves defining abstract factories and products, creating
concrete factories to implement these, and ensuring clients use only the abstract interface.
Benefits:
Drawbacks:
Example: A GUI factory creates buttons and checkboxes tailored for platforms like Windows or
Mac, ensuring platform consistency.
3. Adapter Pattern
Intent: Convert the interface of a class into one expected by clients to enable compatibility.
Motivation: Acts as a bridge for incompatible interfaces, much like real-world adapters for
power supplies or memory cards.
Applicability: Use when an existing class's interface does not match your needs or when
creating reusable classes that interact with unrelated systems.
Structure:
4. Decorator Pattern
Motivation: Adds functionality to objects at runtime, like enhancing a GUI button with borders
or scrollbars.
5. Iterator Pattern
Intent:
Access elements of an aggregate sequentially without exposing its structure.
Motivation:
Separates traversal logic from aggregate objects. Enables multiple traversal methods and
supports generic programming by decoupling algorithms from data structures.
Applicability: Access aggregate elements without exposing internals and Support multiple or
polymorphic traversals.
Structure:
Benefits:
6. Observer Pattern
Intent:
Establish a one-to-many relationship so dependents update automatically when a subject changes
state.
Motivation:
Decouples objects by allowing observers to register with a subject and get notified of changes.
Ensures consistency in dependent objects like GUIs or data models.
Applicability: When one change triggers updates in multiple objects. To avoid tight coupling
between objects.
Structure:
7. Strategy Pattern
Intent:
Define a family of algorithms, encapsulate them, and make them interchangeable to vary
behavior independently from clients.
Motivation:
Provides flexibility by allowing algorithms to be swapped dynamically at runtime without
altering the client. Useful when different behaviors are required for specific contexts or to avoid
hardcoding.
Applicability:
Use when related algorithms need dynamic selection without complicating client code.
Structure:
Example: Sorting strategies (e.g., quick sort, merge sort), payment methods (credit card,
PayPal), or compression techniques (ZIP, RAR).