Design patterns

CS 121
“Ordering Chaos”
Design Patterns
“Mike”
Michael A. Erlinger
Today:


Intro to Topic
http://www.soapatterns.org
designpatterns.f12.ppt
Due This Week
• Finished Proposal
• Prototype
• Review of Phase 1
• 2nd Floor – who is using what?
–2–
CS 121
Design
Practices
Principles
How do we go about design and what do we produce?
What are the characteristics of good design?
LAST TIME
Patterns
What are good solutions to common design problems?
TODAY: Design patterns
–3–
CS 121
Goals
1. Make it easy to build
2. Make it easy to test
3. Make it easy to maintain
4. Make it easy to change
SIMPLE
INTUITIVE
FLEXIBLE
–4–
CS 121
Design principles
Don’t repeat yourself (D.R.Y)
Use real world objects
Single responsibility principle
Encapsulate variation
High cohesion/low coupling
Program to an interface, not an implementation
Law of Demeter (talk only to your friends)
Favor composition over inheritance
Open-closed principle
Liskov Substitution Principle
–5–
CS 121
Design Patterns
Good solution to common problem
Also illustrates good design principles in action
An area of research in both hardware and software for years
–6–
CS 121
Design Patterns
The simplest way to describe a pattern is that it
provides a proven solution to a common
problem individually documented in a
consistent format and usually as part of a
larger collection.
–7–
CS 121
Design Patterns are helpful
because they:
•represent field-tested solutions to common design problems
•organize design intelligence into a standardized and easily "referencable" format
•are generally repeatable by most IT professionals involved with design
•can be used to ensure consistency in how systems are designed and built
•can become the basis for design standards
•are usually flexible and optional (and openly document the impacts of their
application and even suggest alternative approaches)
•can be used as educational aids by documenting specific aspects of system
design (regardless of whether they are applied)
•can sometimes be applied prior and subsequent to the implementation of a system
•can be supported via the application of other design patterns that are part of the
same collection
•enrich the vocabulary of a given IT field because each pattern is given a
meaningful name
–8–
CS 121
Problem: I want a value in
multiple places?....Mike
CLASS: Ball
CLASS: Enemy
Private:
double gravity 32.1
Private:
double gravity 32.1
D.R.Y.!
–9–
CS 121
How about this?
Global double gravity 3.21
– 10 –
CS 121
Why not use globals?
A.
They make code hard to understand.
B.
They make code hard to debug.
C.
They make code hard to modify.
– 11 –
CS 121
Answer
All of the above.
– 12 –
CS 121
Solution: Singleton Pattern
Problem: Ensure a class has only one instance and
provide a global point of access to that instance.
Way to control Global Variables
– 13 –
CS 121
Problem: I want … Lihue2
I want a 2D graphics library that supports the following
functions for triangles:




– 14 –
set color to r,g,b
translate vertices by dx, dy
rotate a degrees about the origin
draw
CS 121
What I Have
I have a 3D graphics library with a triangle class with the following
interface











– 15 –
triangle()
triangle(v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z)
~triangle()
set color(r, g, b)
rotate(vector, angle)
translate(dx, dy, dz)
scale(sx, sy, sz)
draw()
flip(planeA, planeB, planeC, planeD)
texture(textureMap)
standardize()
CS 121
How About: Just use the 3d class
Constructor:
triangle t(v1x, v1y, 0, v2x, v2y, 0, v3x, v3y, 0)
Rotate:
t.rotate(<0,0,1>,alpha)
Interface Segregation Principle
The dependency of one class to another one should depend
on the smallest possible interface.
Liskov principle
– 16 –
CS 121
Solution: Façade Pattern
Problem:
You need to use a subset of a complex
system or you need to interact with the
system in a particular way.
– 17 –
CS 121
Problem: What I want… Linue1
I want a physics engine that (among other things)
detects collisions:
cCollision cPhysicsEngine::detectCollision(cPath p, cTriangles t)
I have a fast collision detection algorithm and
a slower, more robust algorithm.
– 18 –
CS 121
How about this?
cPhysicsEngine
cPhysicsFast
– 19 –
cPhysicsSlow
In the future I may want to use a super slow algorithm.
CS 121
Solution: Strategy Design Pattern
Problem: Want to be able to swap the algorithm used in
an application.
– 20 –
CS 121
Strategy Design Pattern
cPhysicsEngine
encapsulate change
single responsibility
open-closed principle
Favor composition over
inheritance
– 21 –
cDetectCollision
cDetectCollisionFast
cDetectCollisionSlow
CS 121
Problem: I want …
Sycamore
I am developing software that (among other
things) displays geometric primitives
Initially I only need to support lines
In the future I may need to add spheres,
triangles, and squares
Rather than reinvent the wheel, I am going to
use an existing program.
Actually, I really want the option of choosing at
run time between two different drawing
programs, one that is better at low
resolutions and one that is better at high
resolutions
– 22 –
CS 121
…current design
shape
line
draw()
– 23 –
CS 121
Drawing APIs
Package 1:
drawLine(x1, y1, x2, y2)
Package 2:
Notice difference in
Parameter order
drawALine(x1, x2, y1, y2)
– 24 –
CS 121
What about…
line.draw() {
if (resolution == high)
drawLine(v1.x, v1.y, v2.x, v2.y)
else
drawALine(v1.x, v2.x, v1.y, v2.y)
}
– 25 –
CS 121
Comments:
Advantages
simple to implement
 simple to understand

– 26 –
Disadvantages

is knowing about
resolution really a
responsibility of
shape?

D.R.Y.
CS 121
What about
line
lineDP1
– 27 –
lineDP2
CS 121
Comments:
Advantages
simple to implement
 simple to understand

– 28 –
Disadvantages

as additional
shapes and drawing
programs are added
the number of
classes becomes
LARGE
CS 121
Solution: Bridge Design Pattern
Problem: Want to support multiple implementation that
have different interfaces in an extensible way.
– 29 –
CS 121
Solution: Bridge Design Pattern
defines the interface
shapes use to draw
Drawer
Shape
Line
– 30 –
Triangle
Hi Res
Low Res
“adapters” for specific
CS 121
drawing interfaces
Bridge Pattern vs. Strategy
Pattern
Drawer
Shape
cPhysicsEngine
cDetectCollision
cDetectCollisionFast cDetectCollisionSlow
Hi Res
DP hi res
– 31 –
Low Res
DP hi res
Different intents:
• bridge allows implementation to
vary and includes adapters
• strategy allows algorithms
(behavior) to vary
CS 121
Problem: I want …. Rio de Valle
I am building a drawing program. The user
enters keystrokes to change modes (Add,
Delete, Move) and mouse input that is
interpreted based on the current mode.
– 32 –
CS 121
How about this?
What are its disadvantages?
Drawer
processKey
processMouse
AddMode
Drawer
– 33 –
DeleteMode
Drawer
MoveMode
Drawer
CS 121
State Design Pattern
Drawer
processKey
processMouse
1
Add
Mode
processMouse
Delete
Move
supports open-closed and single
responsibility principles
– 34 –
CS 121
State Design Pattern
Drawer
processKey
processMouse
1
Mode
processMouse
1
ModeManager
processKey
Add
1
– 35 –
Delete
Move
1
Mode mgr. returns pointer to correct mode
1
CS 121
Solution: State Design Pattern
Problem: want to allow an object to alter its behavior
when its internal state changes
– 36 –
CS 121
State Pattern vs. Strategy Pattern
Drawer
processKey
processMouse
1
Mode
processMouse
cPhysicsEngine
cDetectCollision
1
ModeManager
processKey
Add
1
Delete
1
Move
cDetectCollisionFast cDetectCollisionSlow
1
Different intents:
• state allows behaviors to vary dynamically
• strategy typically used when algorithm is
selected at start
– 37 –
CS 121
Problem…. Continued…Mike
I also want to support “Undo”
Help!
– 38 –
CS 121
Solution: Command Design
Pattern
Command
Key
– 39 –
Mouse
Menu
CS 121
Command Design Pattern
Encapsulate a request as an object to permit logging,
queuing, un-doing etc.
– 40 –
CS 121
Problem: I want
I want a 2D drawing program that supports
triangle and lines
I want to be able to add, delete, draw, and move
primitives.
I want to be also want to be able to group
primitives into a “widget” and treat the
widget as a primitive.
I want to be able to add and delete primitives
from a widget
– 41 –
CS 121
What about….
Widget
*
Triangle
Shape
Line
What is the difference between a triangle and a widget holding a triangle?
– 42 –
CS 121
Composite Design Pattern
Shape
Triangle
– 43 –
Line
*
Widget
CS 121
Observer Design Pattern
– 44 –
CS 121
Observer Design Pattern
– 45 –
CS 121
Other design patterns
wikipedia!
– 46 –
CS 121
Why Design Patterns?
When you have a thorny problem and you think
someone must have run into it before … think design
patterns
– 47 –
CS 121
The End
– 48 –
CS 121
Solution
Triangle2D
Triangle3D
implements the 2d triangle interface
– 49 –
CS 121