Design Patterns

Report
SWE 316: Software Design and Architecture
Lecture 12
Introduction to Design Patterns
Objectives

Explain the purpose and the general
form of design patterns.
Adapted from Software Design: From Programming
to Architecture by Eric J. Braude (Wiley 2003), with
permission.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Ch 6
Motivation
Design Patterns
Characteristics of DP's
Design Purposes

Reusability, Flexibility, and Maintainability



Reuse flexible designs
Keep code at a general level
Minimize dependency on other classes
 Robustness


Reuse reliable designs
Reuse robust parts
 Sufficiency


2/21
DP's Forms: Delegation and Recursion
/ Correctness
Modularize design
Reuse trusted parts
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
6.1
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
3/21
Motivation
 OOD methods emphasize design notations
 Fine for specification, documentation
 But OOD is more than just drawing diagrams
 Good OO designers rely on lots of experience
 Most powerful reuse is design reuse
 Match problem to design experience
Based on material produced by Prof. Douglas C. Schmidt
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Why do we need Design Patterns?




There is a “designer” and there is a “good designer”
Knowing concepts like abstraction, inheritance, polymorphism,
etc. does not “necessary” make you a good designer
A good designer knows how to combine these concepts to
make flexible, maintainable, and reusable designs.
Using design patterns:


Some body already solved a similar problem
Problem:
capturing, communicating, & applying this knowledge
1
2
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
3
4/21
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Reasons for studying design patterns
1.
2.
3.
4.
5.
6.
7.
8.
Reuse existing, high-quality solutions to commonly recurring
problems.
Establish common terminology to improve communications
within teams.
Shift the level of thinking to a higher perspective.
Decide whether you have the right design, not just one that
works.
Improve individual learning and team learning.
Improve the modifiability of code.
Facilitate adoption of improved design alternatives, even
when patterns are not used explicitly.
Discover alternatives to large inheritance hierarchies.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
5/21
Motivation
Design Patterns
Characteristics of DP's
Students activity
DP's Forms: Delegation and Recursion
(how to do this?)
Design Goal: Flexibility
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
6/21
Motivation
Design Patterns
Characteristics of DP's
What are design patterns?

7/21
DP's Forms: Delegation and Recursion
6.2
Design patterns are class combinations and accompanying
algorithms that fulfill common design purposes

Design pattern expresses an idea
Another Definition
“Design patterns are descriptions of communicating objects
and classes that are customized to solve a general design
problem in a particular context.

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
8/21
DP's Forms: Delegation and Recursion
Design Patterns by Type
6.3
1- Creational patterns:
used to create objects in flexible or constrained ways.
2 - Structural patterns:
used to represent data structures such as trees, with uniform
processing interfaces.
3 - Behavioral patterns:
capturing behavior among a collection of objects.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Types of design patterns (cont.)
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
9/21
Motivation
Design Patterns
Characteristics of DP's
10/21
DP's Forms: Delegation and Recursion
Creational design patterns




These patterns provide guidance on the creation of objects.
They help hide the details of the object instantiation from the
code that uses those objects.
They make a system independent of how its objects are
created, composed and represented.
This leads to low coupling between the users and the way the
objects are created.
Builder
Creational
Patterns
Factory
Abstract
Factory
Prototype
Singleton
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
11/21
DP's Forms: Delegation and Recursion
Structural design patterns

These patterns describe the organization of objects;
that is, how classes and objects are composed to
form larger structures.
Adapter
Bridge
Proxy
Structural
Patterns
Flyweight
Facade
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Decorator
Composite
Motivation
Design Patterns
Characteristics of DP's
12/21
DP's Forms: Delegation and Recursion
Behavioral design patterns




These patterns are concerned with organizing, managing and assigning
responsibilities to objects during execution.
The focus is on communication between the objects involved during some
task.
Typically, these patterns characterize complex control flows that are
difficult to follow at run time.
They therefore help to shift the emphasis away from the low-level flow of
control to the higher level object interactions.
Visitor
Command
Interpreter
Template
Behavioral
Patterns
Strategy
Iterator
Mediator
State
Observer
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Memento
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Characteristics of Design Patterns 1



Viewpoints – ways to describe patterns
1.
Static: class model (building blocks)
2.
Dynamic: sequence or state diagram (operation)
Levels – decomposition of patterns
1.
Abstract level describes the core of the pattern
2.
Concrete (= non abstract) level describes the particulars
of this case
Roles – the “players” in pattern usage
1.
Application of the design pattern itself
2.
Clients of the design pattern application
3.
Setup code initializes and controls
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
13/21
Motivation
Design Patterns
Characteristics of DP's
14/21
DP's Forms: Delegation and Recursion
Characteristics of Design Patterns 2
(class or classes)
1. Client role
3. Role: Application of the design pattern
A. Static viewpoint
A
C
B
D
B. Dynamic viewpoint
(i) Abstract level
(ii) Concrete level
(class model)
2. Setup role
(class or classes)
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
(sequence or
state diagram)
: Reference direction
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Viewpoints & Levels

Two viewpoints


We consider design patterns from the static
viewpoint (what they are made from) and the
dynamic viewpoint (how they function).
Levels

Design patterns usually have an abstract level and a
non-abstract (“concrete”) level.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
15/21
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Design Patterns Forms
 Delegation
 Recursion
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
16/21
6.5
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
17/21
Basic Idea of Delegation
… clientFunction( … )
{
… intermediaryFunction( … ) …
}
… intermediaryFunction( … )
{
… requiredFunction() …
}
Client
clientFunction()
intermediaryFunction()

replace
requiredFunction()
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
18/21
DP's Forms: Delegation and Recursion
Basic Design Pattern Form #1: Delegation
Client
DPInterface
… interfaceMethod( … )
{ … doerObject.doIt() … }
doerObject
DoerBase
interfaceMethod()
ConcreteDoer1
doIt()
doIt()
ConcreteDoer2
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
doIt()
...
Motivation
Design Patterns
Characteristics of DP's
19/21
DP's Forms: Delegation and Recursion
Basic Design Pattern Form #2: Recursion
Client
RecursionBase
doOperation()
NonrecursiveClass
doOperation()
RecursiveClass
aggregate
doOperation()
void doOperation( … )
{ … aggregate … }
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
20/21
DP's Forms: Delegation and Recursion
The Recursion Form Applied to an Organization Chart
Client
Employee
printOrganization()
IndividualContributor
printOrganization()
Supervisor
supervisees
printOrganization()
void printOrganization( … )
{
… supervisees.printOrganization() …
}
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
Motivation
Design Patterns
Characteristics of DP's
DP's Forms: Delegation and Recursion
Summary of This Chapter

Design Patterns are recurring designs satisfying recurring design
purposes

Classified as Creational, Structural, or Behavioral

Described by Static and Dynamic Viewpoints


Typically class models and sequence diagrams respectively
Design patterns Forms usually Delegation or Recursion
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser
21/21

similar documents