Sadp 1
Sadp 1
1. DP – ESSENTIAL ELEMENTS
"Each pattern describes a problem which occurs over and over again in our environment, and then describes the
core of the solution to that problem, in such a way that you can use this solution a million times over, without
ever doing it the same way twice"
1. Pattern Name
• The name of a pattern acts like a "handle" to describe a design problem, its solution, and consequences
in just a word or two.
• It makes it easier to discuss designs and trade-offs with others.
• Example in MVC: "Model-View-Controller (MVC)" is the name of the design pattern. This name
makes it easier to communicate about how to structure user interfaces effectively.
2. Problem
• This explains when and why to use the pattern. It describes the context and the specific challenge.
• Helps you understand the situations where the pattern is applicable.
• Example in MVC: The problem is how to build a user interface where the visual representation (views)
and the core logic or data (model) are separated. This separation ensures flexibility, such as updating one
part without affecting others.
3. Solution
• Describes the components of the pattern (objects, classes, and their relationships) and how they work
together.
• Provides a template for solving a problem without dictating the exact implementation.
• Example in MVC: In MVC:
o Model: Holds the data and core logic.
o View: Displays the data visually (e.g., charts, tables).
o Controller: Manages user input and defines how the interface responds to it.
o They interact via a subscribe/notify mechanism:
▪ The model notifies views of changes.
▪ The views update their display accordingly.
4. Consequences
• These are the results and trade-offs of using the pattern. It might include space, time, flexibility, or
extensibility considerations.
• Helps you evaluate if the pattern is a good fit for your project.
• Example in MVC:
o Benefits: Flexibility to attach multiple views to the same model (e.g., a spreadsheet, pie chart,
and histogram can all show the same data differently). Easy to reuse components.
o Trade-offs: Increased complexity because of the eed to manage interactions between the model,
views, and controllers.
1. Identify the Problem: Understand the specific problem you're trying to solve. Focus on challenges such
as tight coupling, frequent redesign needs, or inflexible structures in your application.
Example: If you're dealing with a UI where changes in one component should notify others (e.g., a
dashboard updating when data changes), consider the Observer Pattern.
2. Consider Design Pattern Categories: Patterns are categorized into Creational, Structural, and
Behavioral:
a. Creational: Handle object creation efficiently (e.g., Singleton, Factory).
b. Structural: Deal with the arrangement of classes/objects (e.g., Adapter, Composite).
c. Behavioral: Define communication between objects (e.g., Strategy, Observer).
Example: Use the Factory Method to abstract object creation if you want to avoid specifying concrete
classes.
3. Focus on What Should Be Variable: Instead of worrying about possible changes, identify what needs
to remain flexible. Design patterns help isolate variability.
Example: To vary an algorithm without changing its client, use the Strategy Pattern.
4. Scan Pattern Intent Sections: Review the intent of patterns to see if they match your problem. For
instance, if the problem involves creating objects without specifying their classes, Abstract Factory is a
good match.
5. Check Pattern Interrelationships: Some patterns complement each other. For example, Decorator can
be combined with Composite to handle hierarchical structures with additional functionality.
6. Examine Causes of Redesign: Refer to common redesign triggers such as tight coupling, reliance on
specific implementations, or difficulty extending functionality. Patterns like Adapter or Decorator can
address these issues.
1. Read the Pattern Overview: Start with the Applicability and Consequences sections to ensure it's
suitable for your problem.
2. Understand Key Elements: Study the Structure, Participants, and Collaborations to understand how
classes and objects interact in the pattern.
3. Analyze Example Code: Review sample implementations to learn how to apply the pattern practically.
4. Adapt Participant Names: Rename classes and methods in the pattern to suit your application's
context.
5. Define Classes and Interfaces: Set up the required classes, declare their interfaces, and define their
relationships.
6. Implement Pattern Logic: Write the code to carry out the responsibilities and collaborations defined by
the pattern.
7. Test and Refine: Ensure the pattern integrates well with your application and solves the intended
problem without introducing unnecessary complexity.
This template ensures that each design pattern is well-documented and easy to understand, facilitating its
application in various scenarios.
4. HOW PATTERN CATALOG IS ORGANIZED?
The Pattern Catalog is organized to make it easier to learn and apply design patterns by classifying and
presenting them systematically. Design patterns have different levels of complexity and abstraction, so they
need to be organized. Patterns are classified by two main criteria:
Delegation is a design principle in object-oriented programming where an object hands off (delegates) a request
or responsibility to another object. Instead of inheriting behavior from a parent class, an object achieves
functionality by referencing another object and forwarding requests to it.
• Example: Instead of making a Window class inherit from Rectangle, the Window class can include a
Rectangle instance and delegate tasks like calculating the area to it. This shifts from an "is-a"
relationship (inheritance) to a "has-a" relationship (composition).
Advantages of Delegation
1. Runtime Flexibility: Behaviors can be composed dynamically at runtime, offering greater flexibility.
a. Example: A Window can dynamically delegate specific responsibilities to different objects based
on the current context.
2. Encapsulation Preservation: Since objects interact through defined interfaces, their internal details
remain hidden, maintaining encapsulation.
3. Reusability: Delegation supports code reuse by combining existing objects' functionality in new ways
without creating complex inheritance hierarchies.
4. Avoids Tight Coupling: Delegation decouples the delegating object from the behavior implementation,
making the system easier to modify and extend.
Disadvantages of Delegation
1. Increased Complexity: Delegation can make the system harder to understand due to the additional
layers of indirection and dynamic composition.
2. Performance Overhead: Forwarding requests to other objects introduces an additional level of
indirection, which might slightly impact performance.
3. Potential for Overuse: If used excessively, delegation can lead to systems with many small,
interconnected objects, complicating maintenance and debugging.
Design Patterns and Frameworks are both powerful tools in software development, but they serve different
purposes and operate at different levels of abstraction.
Design Patterns
2. Bridge: Decouples an abstraction from its implementation so that the two can vary independently.
3. Command: Encapsulates a request as an object, thereby letting you parameterize clients with different
requests, queue or log requests, and support operations that undoable.
4. Composite: Composes objects into tree structures to represent part-whole hierarchies. Lets clients treat
individual objects and compositions of objects uniformly.
5. Decorator: Dynamically adds responsibilities to objects. Provides a flexible alternative to subclassing for
extending functionality.
6. Facade: Provides a unified interface to a set of interfaces in a subsystem. Defines a higher-level interface that
makes the subsystem easier to use.
7. Factory Method: Defines an interface for creating an object, but lets subclasses decide which class to
instantiate. Let's a class defer instantiation to subclasses.
Drawbacks
• Increased complexity due to multiple layers of abstraction.
• Object creation and destruction can add performance overhead.
• Steeper learning curve for developers new to OOP.