Professional Documents
Culture Documents
2011 05-May 11 DesignPatterns 01
2011 05-May 11 DesignPatterns 01
These slides are NOT to be used as a replacement for student notes. These slides are sometimes vague and incomplete on purpose to spark class discussions
GangofFour(GoF) OODesignPatterns
CS446/646ECE452 May11th,2011
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
Motivation
Object Oriented Analysis (OOA)
addresses the functional challenges what a system does provides guidance for implementation
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
Motivation
How can we improve OOD
generic blueprints (micro architecture) language and implementation independent two main catalogues
GoF: Gang of Four (Gamma, Helm, Johnson, Vlissides, 1995) POSA: Pattern Oriented Software Architecture (Buschmann, et al.; Wiley, 1996)
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
Motivation
What is a Design Pattern
Anatomy
name problem/motivation solution consequences & tradeoffs which ones are important for us?
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
DesignPatternsClassification
GoF Design Patterns Creational Structural Behavioral
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
DesignPatternsClassification
GoF Design Patterns Creational Structural
Adaptor - class
Behavioral
Interpreter Template Method
Factory Method
Singleton
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
DesignPatternsClassification
Purpose based classification
creational:
concerns with creation process of objects & classes composition of classes & objects characterizes interaction & responsibility of objects & classes
structural
behavioral
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
DesignPatternsClassification
Scope based classification
Two categories
class scope
relationship between classes & subclasses statically defined at run-time object relationships (what type?) can be manipulated at runtime (so what?)
CS446/646 ECE452 8
object scope
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
DesignPatternsClassification
Creational class
Creational object
defers object creation to sub-classes (factory method) inheritance to compose classes (adapter) uses inheritance to describe flow of control, algorithms (template)
defers object creation to other objects (abstract factory) deals with object assembly (adapter) group of objects working together to carry out a task (iterator)
9
Structural class
Structural object
Behavioral class
Behavioral object
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
Singleton
Intent
ensure a class only has one instance, and provide a global point of access to it.
Construction
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
10
Singleton
Intent
ensure a class only has one instance, and provide a global point of access to it.
Construction
public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor prevents // instantiation from other classes private Singleton() {} public static Singleton getInstance() { return INSTANCE; }
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
11
Singleton
Advantages
can dictate who, and when a client can access via inheritance/subclass
refinement of functionality
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
12
Singleton
Advantages
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
13
Singleton
A closer look at Singleton
reuse separation of concerns global presence stateful vs. stateless multiple instances life cycle
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
14
SingletonACloserLook
Reuse
coupling
results in tighter coupling couples with the exact type of the singleton object pass by reference to reduce coupling?
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
15
SingletonACloserLook
Reuse
coupling
results in tighter coupling couples with the exact type of the singleton object pass by reference to reduce coupling?
public void doSomething(){ Worker worker = Worker.getInstance(); worker.perform(); } public void doSomething(Worker worker){ worker.perform(); }
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
16
SingletonACloserLook
Reuse
inheritance
easy to extend functionality in subclasses not easy to override the object instance in subclasses
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
17
SingletonACloserLook
Separation of concerns
database connection as a singleton system 1 uses a singleton to ensure only a single database connection system 2 needs to connection pool of 10 databases connections
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
18
SingletonACloserLook
Global presence
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
19
SingletonACloserLook
Stateful singleton
aren't global variables bad? synchronization concurrency multiple threaded using a singleton
access concerns
mutable vs. immutable state better then stateful can we have a stateless singleton?
CS446/646 ECE452 20
Stateless singleton
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
SingletonACloserLook
Multiple instances
distributed systems
is it possible to have a true singleton in a distributed system? global registries/repositories initialization has to be thread safe serialization class loaders
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
21
SingletonACloserLook
Life-cycle & life span
creation
lazy initialization as long as an application's life span registries can outlive applications unit testing requires short lived state reloading singleton class (servlets) loss of state
CS446/646 ECE452 22
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
Singleton
When can I use a singleton
considerations[1]
will every user use this class exactly the same way? will every applications ever need only one instance? should the clients be unaware of the application Java Math class (stateless static class) top level GUI (window/frame) logging
examples
[1] http://www.ibm.com/developerworks/library/co-single.html
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
23
Adapter
Intent
convert the interface of a class into another interface... Adapter lets classes work together that couldn't otherwise because of incompatible interface also known as wrapper boolean values can be represented by
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
24
Motivation
Need to add Text capability to our drawing editor. Consider an off the shelf TextView component
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
25
Motivation
Observations
Shape provides interface and TextView provides an implementation Lets try to draw this?
CS446/646 ECE452 26
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
AdapterClass
Requirement
WATERLOO
AdapterObject
Requirement
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
28
AdapterClassvs.Object
Class
Object
including sub-classes
why?
static in nature
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
29
AdapterClassvs.Object
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
30
Adapter&DependencyInversion
Dependency Inversion (DI)
Business layer
Abstract Layers
CS446/646 ECE452
31
DependencyInversionExample
Implications
Button implementation relies on Lamp any changes to Lamp will impact Button what if we want to reuse Button class with a different component
such as a motor
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
32
DependencyInversionExample
Dependency Inversion to Rescue
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
33
DependencyInversionExample
Observation
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
34
Adapter
How much adaptation is reasonable?
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
35
Bridge
Intent
decouples an abstraction from its implementation so the two can vary independently does this not sounds like an adapter?
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
36
Bridge
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
37
Bridge
Bridge
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
38
BridgeExample
Problem
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
39
BridgeExample
Solution: Use bridge pattern to place abstraction and implementation in two different hierarchies
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
40
BridgeExample
Solution: Use bridge pattern to place abstraction and implementation in two different hierarchies
Bridge
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
41
Bridge
Features
flexible binding between abstraction & implementation two class hierarchies clients are decoupled
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
42
Adapter&Bridge
Common Elements
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
43
Adapter&Bridge
Difference in intent
adapter
resolves incompatibilities between two existing interfaces two interfaces are independent and can evolve separately coupling is unforeseen adapts components after they have been designed connects an abstraction and its many implementations evolution is in accordance with the base abstraction coupling between the abstraction and the implementations are known
bridge
WATERLOO
CHERITON SCHOOL OF COMPUTER SCIENCE
CS446/646 ECE452
44