Design_Patterns_Study_Sheet-1
Design_Patterns_Study_Sheet-1
Observer Pattern
Purpose: Defines a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically.
Key Concept: Publish-subscribe mechanism where observers register with a subject and
are notified of state changes.
When to Use:
• Subject: Maintains a list of observers and provides methods to attach, detach, and
notify them.
• Observer: Defines an interface for receiving update notifications from the subject.
Pros:
Cons:
Decorator Pattern
Key Concept: Objects are wrapped with other objects (decorators) that add behavior
before or after delegating calls to the wrapped object.
When to Use:
• Component: Defines the interface for objects that can have additional
responsibilities.
Pros:
Cons:
Purpose: Defines an interface for creating an object but allows subclasses to alter the type
of objects that will be created.
Key Concept: Creation is delegated to subclasses by overriding the factory method.
When to Use:
Pros:
Cons:
Key Concept: Uses composition to delegate object creation to multiple factory methods
grouped in a single factory interface.
When to Use:
• When the system should be independent of how its products are created.
Pros:
Cons:
Singleton Pattern
Purpose: Ensures a class has only one instance and provides a global point of access to it.
Key Concept: The class is responsible for controlling its own instance through a private
constructor and static access method.
When to Use:
Pros:
• Subclassing is difficult.
Command Pattern
Key Concept: A command object encapsulates an action and its receiver, exposing an
execute() method to trigger the action, and optionally undo() for reversibility.
When to Use:
• Receiver: Knows how to perform the operation associated with the command.
• Invoker: Stores command objects and triggers them when needed (e.g., a button
click).
Pros:
Cons:
Adapter Pattern
Purpose: Converts the interface of a class into another interface clients expect. Enables
incompatible interfaces to work together.
Key Concept: Acts as a translator between a client and an incompatible class (adaptee),
wrapping the adaptee’s methods in the target interface.
When to Use:
• When you want to reuse an existing class, but its interface is incompatible.
• Adapter: Implements the target interface and delegates calls to the adaptee.
Pros:
Cons:
Facade Pattern
When to Use:
Pros:
Cons:
Key Concept: A template method contains fixed parts of an algorithm, calling abstract or
hook methods for customizable behavior.
When to Use:
• When you have multiple classes performing similar steps in the same order.
• Template Method: Contains a fixed algorithm with calls to abstract and/or hook
methods.
• Hook Methods: Optional methods that subclasses can override to extend behavior.
Pros:
Cons:
• Open-Closed Principle (OCP): A module or class should be open for extension but
closed for modification. In practice, this means adding new features should be done
by adding new code (like subclasses or decorators), not modifying existing code.
• Global State: Global state refers to data accessible from many parts of the system.
While convenient, it can cause hidden dependencies, reduce modularity, and make
debugging and testing difficult.
• Null Object: A design pattern that provides a default object with neutral behavior in
place of a null reference. It prevents null pointer exceptions and simplifies code that
relies on optional behavior.
• Hollywood Principle: “Don’t call us, we’ll call you.” Higher-level components
control the flow of the algorithm and invoke lower-level components when needed.
This is central to the Template Method Pattern.
Singleton Pattern
• Multithreading Details:
• Advantages:
• Caveats:
• Clear comparison:
o Explains it’s not a formal GoF pattern but a commonly used idiom.
• Design Principles Connection:
Study Sheet doesn't offer this depth of comparison or design principle integration.
Decorator Pattern
• Implementation Notes:
• Real-world examples:
o Java I/O streams, GUI features, and Starbuzz Coffee example (adding
condiments dynamically).
o Explains how using new directly couples code to concrete classes and breaks
encapsulation.