Chapter 6: Introduction to Design Patterns

Chapter 6:
Introduction to Design Patterns
King Fahd University of Petroleum & Minerals
SWE 316: Software Design & Architecture
Semester: 072
Objectives
z
To introduce design patterns and their types,
characteristics, and forms
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
2
What is a Pattern?
z
"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" [Christopher
Alexander]
z
In general, a pattern has four essential elements:
z
z
z
z
Name
Problem - When to apply the pattern
Solution - Elements that make up the pattern, their
relationships, responsibilities, and collaborations.
Consequences - Results and trade-offs of applying the pattern
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
3
What is a Design Pattern?
z
Design patterns are class combinations and
accompanying algorithms that fulfill common design
purposes.
z
Design patterns are descriptions of communicating
objects and classes that are customized to solve a
general design problem in a particular context.
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
4
Types of Design Patterns
z
Creational patterns (Chapter 7)
z
z
Structural patterns (Chapter 8)
z
z
Creating a collection of objects in flexible ways.
Representing a collection of related objects.
Behavioral patterns (Chapter 9)
z
Capturing behavior among a collection of objects.
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
5
Characteristics of Design Patterns
z
Viewpoints – ways to describe patterns
z
z
z
Levels – decomposition of patterns
z
z
z
Static: class model (building blocks)
Dynamic: sequence or state diagram (operation)
Abstract level describes the core of the pattern
Concrete (= non abstract) level describes the particulars of
this case
Roles – the “players” in pattern usage
z
z
z
Application of the design pattern itself
Clients of the design pattern application
Setup code initializes and controls
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
6
Characteristics of Design Patterns (Cont’d)
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
7
Exercise:
Characteristics of Design Patterns
z
Consider the Observer design pattern class model shown below.
z
z
Group the classes to show abstract and concrete levels.
Group the classes to show client, setup and application roles.
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
8
Design Pattern Forms
z
Delegation
z
z
z
An implementation mechanism in which an object forwards
(delegates) a request to another object.
The delegate carries out the request on behalf of the original
object.
Recursion
z
z
Part of the pattern essentially uses itself
Example: constructing graphical user interfaces that allow for
windows within window within window …
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
9
The Delegation Design Pattern Form
z
Basic idea of delegation
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
10
The Delegation Design Pattern Form
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
11
The Recursion Design Pattern Form
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
12
Example: The Recursion Form Applied
to an Organization Chart
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
13
Summary
z
Design Patterns are recurring designs satisfying recurring design
purposes
z
Described by Static and Dynamic Viewpoints
z
z
Typically class models and sequence diagrams respectively
Use of a pattern application is a Client Role
z
z
Client interface carefully controlled
“Setup,” typically initialization, a separate role
z
Design patterns Forms usually Delegation or Recursion
z
Classified as Creational, Structural, or Behavioral
SWE 316 (072)
Chapter 6: Introduction to Design Patterns
14