17-Design Patterns Introduction
17-Design Patterns Introduction
17-Design Patterns Introduction
While design patterns offer numerous benefits, it's important to be aware of potential drawbacks and
considerations:
1. Complexity:
- Overusing design patterns can lead to overly complex and convoluted code. Not every problem
requires a design pattern and applying them unnecessarily can make the code harder to understand and
maintain.
2. Rigidity:
- Design patterns, if applied too rigidly, may make the system inflexible to change. In some cases,
adhering strictly to a pattern may hinder the ability to adapt the software to evolving requirements.
3. Applicability:
- Not all design patterns are suitable for every situation. Choosing the wrong pattern for a particular
problem can lead to unnecessary complexity and reduced code efficiency.
4. Overhead:
- Introducing design patterns can introduce additional layers of abstraction and indirection, which may
result in some performance overhead. In performance-critical applications, this can be a concern.
Drawbacks
5. Anti-patterns:
- Misusing or misunderstanding design patterns can lead to the creation of anti-patterns—solutions
that seem to solve a problem but introduce more issues. This can happen when a pattern is applied
without a clear understanding of its intent.
6. Code Bloat:
- In some cases, design patterns may lead to code bloat, where the structure of the pattern adds more
lines of code than a simpler, more direct solution would. This can impact readability and maintenance.
7. Lack of Creativity:
- Relying too heavily on design patterns might stifle creativity in problem-solving. Developers may
become accustomed to applying known patterns without considering innovative or more efficient
alternatives.
Design Patterns MVC
• Model is the application object
• View is the model's screen presentation
• Controller defines the way the user interface reacts to user input.
• MVC decouples views and models by establishing a subscribe/notify
protocol between them.
MVC (Model View Controller)
MVC (Model View Controller)
MVC (Model View Controller)
Describing Design Patterns
• Pattern Name and Classification
• Intent
• What does the design pattern do? What is its rationale and intent? What particular
design issue or problem does it address?
• Also Known As (Other well-known names)
• Motivation
• A scenario that illustrates a design problem and how the class and object structures in
the pattern solve the problem.
• Applicability
• What are the situations in which the design pattern can be applied?
• Structure
• A graphical representation of the classes in the pattern using a notation based on UML.
• Participants
• The classes and/or objects participating in the design pattern and their responsibilities.
Describing Design Patterns
• Collaborations
• How the participants collaborate to carry out their responsibilities.
• Consequences
• How does the pattern support its objectives? What are the trade-offs and results of using the
pattern?
• Implementation
• What should you be aware of when implementing the pattern? Are there language-specific
issues?
• Sample Code
• Code fragments that illustrate how you might implement the pattern in particular object-
oriented programming languages.
• Known Uses
• Examples of the pattern found in real systems.
• Related Patterns
• What design patterns are closely related to this one? What are the important differences?
With which other patterns should this one be used?
Catalog of Design Patterns
Purpose
• The first criterion, called purpose, reflects what a pattern does.
• Patterns can have either creational, structural, or behavioral purpose.
There are three main groups of patterns:
• Creational patterns provide object creation mechanisms that increase
flexibility and reuse of existing code.
• Structural patterns explain how to assemble objects and classes into larger
structures, while keeping these structures flexible and efficient.
• Behavioral patterns take care of effective communication and the
assignment of responsibilities between objects.
Scope
• The second criterion, called scope, specifies whether the pattern applies
primarily to classes or to objects.
• Class patterns deal with relationships between classes and their
subclasses. These relationships are established through inheritance, so
they are static—fixed at compile-time.
• Object patterns deal with object relationships, which can be changed at
run-time and are more dynamic.
Reuse Mechanism
Inheritance versus Composition
• The two most common techniques for reusing functionality in object-oriented systems are class inheritance
and object composition.
• Class Inheritence lets you define the implementation of one class in terms of another’s.
• Reuse by subclassing is often referred to as white-box reuse.
• The term "white-box" refers to visibility: With inheritance, the internals of parent classes are often visible to
subclasses.