0% found this document useful (0 votes)
13 views5 pages

Design Patterns Theory

Uploaded by

amnaazeem290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Design Patterns Theory

Uploaded by

amnaazeem290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

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:

 Controlled access to a single instance.


 Reduced need for global variables.
 Easily extensible for multiple instances when required.

Drawbacks:

 Hidden dependencies and tight coupling.


 Makes unit testing harder.

Example: A logger class provides a global access point for logging across the application
without multiple instances.

2. Abstract Factory Pattern

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:

 Promotes consistency across related products.


 Simplifies swapping between product families.

Drawbacks:

 Adding new products can require significant changes.

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:

 Target: Expected interface.


 Adapter: Existing class.
 Adapter: Converts Adapter’s interface into Target’s.
 Client: Uses Target interface.

Benefits: Reuses existing classes and decouples systems.


Drawbacks: Adds complexity with extra layers.
Example: Wrapping third-party APIs to fit application architecture.

4. Decorator Pattern

Intent: Dynamically add responsibilities to an object without modifying its structure.

Motivation: Adds functionality to objects at runtime, like enhancing a GUI button with borders
or scrollbars.

Applicability: Use when adding dynamic responsibilities or when subclassing is impractical.


Structure:

 Component: Base interface/class.


 ConcreteComponent: Object to decorate.
 Decorator: Wraps the object.
 ConcreteDecorator: Adds new behavior.

Benefits: Flexible and avoids heavy base classes.


Drawbacks: Increases complexity and debugging difficulty.
Example: Adding toppings to ice cream, each acting as a decorator.

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:

 Iterator: Interface for traversal.


 ConcreteIterator: Tracks the current position.
 Aggregate: Interface to create iterators.
 ConcreteAggregate: Creates specific iterators.

Benefits:

 Clean aggregate interface.


 Multiple, flexible traversals.

Drawbacks: Adds complexity with extra classes.

Example: Traversing arrays, linked lists, or trees uniformly.

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:

 Subject: Manages and notifies observers.


 ConcreteSubject: Stores state and triggers updates.
 Observer: Defines update interface.
 ConcreteObserver: Syncs state with the subject.

Benefits: Loose coupling, reusable objects.

Drawbacks: Notification overhead, potential stale data.

Example: MVC frameworks, event listeners, dashboards.

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:

 Strategy: Interface for algorithms.


 ConcreteStrategy: Implements a specific algorithm.
 Context: Uses a Strategy to execute behavior.
Benefits:

 Simplifies code by removing conditionals.


 Enhances flexibility and reusability.

Drawbacks: Adds objects, increasing complexity.

Example: Sorting strategies (e.g., quick sort, merge sort), payment methods (credit card,
PayPal), or compression techniques (ZIP, RAR).

You might also like