Engineering of Software II: Spring 2003
Engineering of Software II: Spring 2003
of Software II
Spring 2003
Why Design Patterns?
Problems
The hard part about object-oriented design is
decomposing a system into objects
The task is difficult because many factors influence the
decomposition, often in conflicting ways:
encapsulation,
granularity,
dependency,
aggregation,
flexibility,
performance,
evolution,
reusability
Why Design Patterns?
Solutions
Expressing proven techniques as design patterns makes
them more accessible to developers of new systems.
Design patterns:
Make it easier to reuse successful designs and
architectures
Can even improve the documentation and maintenance
of existing systems
Help you identify less-obvious abstractions and the
objects that can capture them
Put simply, design patterns help a designer get a design
"right" faster.
Introduction to Design
Patterns
History of Design Patterns
Summary of Patterns
Characterizing Design Patterns
Visualizing Design Patterns
Creational Design Patterns - concern the process
of object creation
Behavioral Design Patterns - deal with the
composition of classes or objects
Structural Design Patterns - characterize the ways
in which classes or objects interact and distribute
responsibility
History of Design Patterns
The history of design patterns in object-oriented programming
is most often attributed to Erich Gamma, the initiating author
of “Design Patterns: Elements of Reusable Object-Oriented
Software.”
The true father of design patterns is Christopher Alexander,
an architect educated in mathematics, science, and
engineering.
Alexander believes you can design a building by simply
applying one pattern after another. His theory does not deny
the necessity of creativity in design and implementation, but
his precise description of how patterns generate design is a
clear implication that a pattern language can make the design
process deterministic and repeatable.
Model View Controller
(MVC)
Summary of Patterns
Purpose
Scope: Class patterns deal with static relationships between classes and their subclasses, which
are fixed at compile-time. Object patterns deal with dynamic object relationships, which can
be changed at run-time.
Characterizing Design Patterns
1. name to communicate the design element.
2. problem describes when to apply the pattern.
3. solution describes the elements that make up the
design, their relationships, responsibilities, and
collaborations. The solution doesn't describe a
particular concrete design or implementation,
because a pattern is like a template that can be
applied in many different situations.
4. consequences are the costs/benefits of applying
the pattern.
Characterizing Design Patterns
More Detailed Characterization
Intent Consequences
Also Known As Implementation
Motivation Sample Code
Applicability Known Uses
Structure Related Patterns
Participants
Collaborations
Visualizing Design Patterns
Initially, the Object Modeling Technique
(OMT) was used to represent design
patterns, however, the Unified Modeling
Language (UML) has replaced OMT.
UML captures real-world concepts (objects),
their attributes, and the associations between
these concepts.
UML is used to visualize and represent
design patterns.
Creational Design Patterns
Creational Design Patterns abstract the
instantiation process
By abstracting the instantiation process,
the system is independent of how its
objects are created, composed, and
represented
Creational Patterns can be implemented
by objects, or by class inheritance
Creational Design Patterns
Encapsulates knowledge about which
concrete classes a system uses
Hides how objects are instantiated and
how they are composed
Creational patterns are closely related,
sometimes they complement one and
another or are competitors
Behavioral Design Patterns
Behavioral patterns are concerned with
algorithms and the assignment of
responsibilities between objects
Behavioral patterns describe patterns of
objects and classes, but also the
communication between them
Behavior patterns are implemented using
inheritance and object composition
Behavioral Design Patterns
Behavioral patterns use inheritance to
distribute behavior between classes
Behavioral object patterns use object
composition rather than inheritance
Some describe how a group of peer objects
cooperate to perform a task that no single
object can carry out by itself
Other behavioral object patterns are concerned
with encapsulating behavior in an object and
delegating requests to it
Structural Design Patterns
Structural Patterns are concerned with how
classes and objects are composed to form
larger structures
Structural class patterns use inheritance to
compose classes and implementations
Structural objects describe ways to compose
patterns to realize new functionality
Structural patterns are related to some
degree
Introducing Design Patterns into
the Data Structures Class
Labs – Patterns introduced in laboratory
exercises
Allow simple hands on experience using patterns
Demonstrate the power and usefulness of patterns
Lectures – Patterns introduced in lecture,
and explained by example
Allow the introduction of design patterns used by
Java but not used directly in the course
Tests – Students evaluated on pattern
knowledge
Introduction by Labs
Lab Description Design
# Patterns
1 Program using an Abstract Data Type Dense List implementation of a Iterator,
List interface Factory
method
2 Same program substituting Linked list implementation of a List interface Iterator,
Factory
method
3 Program sorting data using a Linked List implementation of a Priority Enumerator,
Queue (n2 sort) Adapter
4 Program to convert infix equations into prefix form using Stacks and Singleton
Queues (various implementations)
5 Program sorting data using the Heap Sort with a Dense List tree. nLog 2n Decorator
sort
6 Program to insert, search, traverse and delete a binary tree using nodes Visitor
with links
Lab 1 – Dense List
OBJECTIVE:
To introduce the Abstract Data Type (ADT)
To introduce the class implementation of a
ADT
To reinforce simple Java Applications,
interfaces, and problem domain classes
To reinforce inheritance and composition
To introduce design patterns: Factory Method
(Creational) and Iterator (Behavioral)
Lab 1 – Dense List
Lab 2 – Link List
OBJECTIVE:
To demonstrate the Abstract Data Type (ADT)
implementation independence
To introduce the dynamic linked list implementation of
a basic data structure
To introduce the pointer concept
To reinforce simple Java Applications
To reinforce inheritance, polymorphism, and
composition
To reinforce design patterns: Factory Method and
Iterator
Lab 2 – Link List
Lab 3 – Priority Queue
OBJECTIVE:
To introduce a sorting technique (priority
queue sorting)
To introduce the measurement of algorithm
performance
To reinforce the Abstract Data Type (ADT)
To reinforce the class implementation of a ADT
To introduce the Enumeration (Behavioral) and
Adapter (Structural) design patterns
Lab 3 – Priority Queue
Lab 4 – Prefix Parser (Stack
and Queue implementations)
OBJECTIVE:
To introduce the stack and queue ADT.
To reinforce the structured approach to
programming
To reinforce the Abstract Data Type (ADT)
To reinforce the class implementation of a ADT
To introduce the concept of a Singleton
Pattern (Creational)
To introduce the concept of a History List
Lab 4 – Prefix Parser (Stack
and Queue implementations)
Lab 5 – Iterative Heap Sort
OBJECTIVE:
To reinforce the measurement of algorithm
performance
To introduce a sorting technique (heap sorting)
To reinforce the concept of an Iterator pattern
(Behavioral)
To reinforce the concept of an Factory Method pattern
(Creational)
To introduce the concept of Decorator pattern
(Structural)
To reinforce the concept of Code Reuse
Lab 5 – Iterative Heap Sort
Lab 6 – Binary Tree
To introduce a binary tree
implementation
To introduce tree operations (insert,
search, delete, and list)
To introduce basic Audit Trail
techniques
To introduce the Visitor Pattern
(Behavioral)
Example Sample Code
The Factory Method Pattern
public AbstractIterator getIterator()
{
return new SomeIterator(listADT);
}