Title: Design Patterns
1Design Patterns
- William A. Hoffman
- NYU OOP Class
2Design Patterns Overview
- Why Design Patterns?
- History
- Definitions / Terminology
- Pattern Examples - from DP book
- Summary
3Why OO Design Patterns
- Effective for teaching OOD
- OOP is a new Art form
- People have only had 20 years experience with
OOP - Architecture and painting have been around for
thousands of years. - Effective method of transferring skill and
experience - new OO programmers can be overwhelmed by the
options
4History of Design Patterns
- Christopher Alexander - an architect
- The Timeless Way of Building, 1979
- A Pattern Language, 1977
- Pattern - A solution to a problem in a context
- Purposes
- effective reuse
- dissemination of solutions
5History in OOP
- 1987 workshop at OOPSLA Beck and Ward
- 1993 The Hillside Group - Beck, Ward, Coplien,
Booch, Kerth, Johnson - 1994 Pattern Languages of Programming (PLoP)
Conference - 1995 Design Patterns Elements of Reusable OO
software - Gamma, Helm, Johnson, Vlissides (Gang
of Four)
6Definitions / Terminology
- Pattern Language - a term from Christopher
Alexander, not a software language but a group of
patterns used to construct a whole - Pattern - many definitions see FAQ, lets try this
one Patterns represent distilled experience
which, through their assimilation, convey expert
insight and knowledge to inexpert developers.
- Brad Appleton
7Types of Software
- Application Programs - internal reuse, extension.
Excell, PowerPoint - Toolkits - set of related, reusable general
purpose classes. (Fresco) - Frameworks- reusable set of cooperating classes
for a specific class of software (TargetJr)
8How a Pattern is Defined(GoF form)
- Name - good name
- Intent- what does it do
- Also Known As
- Motivation - a scenario
- Applicability - when to use
- Structure- UML
- Participants - classes
- Collaborations - how they work together
- Consequences - trade offs
- Implementation- hints on implementation
- Sample Code
- Known Uses
- Related Patterns
9New Patterns
- Rule of 3, should be used in 3 places, it should
be a reoccurring pattern, not an algorithm. - Each Domain has patterns
10Purpose of Design Patterns
- Creational - Abstact Factory
- Structural - Adapter, Bridge
- Behavioral - Iterator, State
11Singleton (creational)
- Intent - ensure a class has only one instance and
provide a global point of access - Motivation - Some classes must only have one
instance, (one file system, one window mgr),
allow class to ensure only one instance. - Applicability
- Must be only one instance
- The one instance should be extensible by
subclassing, and clients should be able to use
extended version without modification of client
12Singleton Cont.
Singleton
return uniqueInstance
static Instance() SingleOperation() GetSingleton
Date()
static uniqueInstance singletonData
13Singleton Cont
- Participants
- Singleton
- defines an Instance operation that lets clients
access its unique instance, a static or class
method is used - Collaborations
- clients access only from the unique instance
14Singleton Cont
- Consequences
- controlled access to sole instance
- reduced name space (no globals)
- permits refinement via sub-classing
- permits a variable number of instnaces
- more flexible than class operations, specifically
in c/java static is not virtual - clients access only from the unique instance
15class Singleton public static Singleton
Instance() protected Singleton() private
static Singleton _instance Singleton
Singleton_instance 0 Singleton
SingletonInstance() if(!_instance)
_instance new Singleton() return _instance
16Singleton Cont
- Sample Code - mazefactory class
- Known Uses
- meta class
- InterViews WidgetKit
- Related Patterns
- many patterns use singleton See Abstarct
Factory, Builder and Prototype
17Proxy
- A surrogate or placeholder for another object to
control access to
Client1 Image_proxy
TCIP
ORB Image (instance)
Client2 Image_proxy
18CacheProxy
Client1 Image_proxy_wcache
ORB Image (instance)
Client2 Image_proxy_wcache
Can use Observer Pattern to Notify clients of
changes in the source image class
19Interaction of Patterns
- Patterns are often interelated
- AbstractFactory is often done as a Singleton
- Observer and Proxy in CORBA
20Template Method
- Intent - Define a skeleton of an algorithm in an
operation, deferring some steps to subclasses.
Sub-classes can redefine steps in an algorithm
without changing its structure - Motivation- Document example
- Applicability
- implement invariant parts of an algorithm
- avoid code duplications by noticing common
behavior in sub-classes - hook extenstions
21AbstractClass
TemplateMethod() PrimativeOp1() PrimativeOp2()
PrimativeOp1() PrimativeOp2() ...
ConcreateClass
PrimativeOp1() PrimativeOp2()
22 // From NeXT AppKit void ViewDisplay()
SetFocus() DoDisplay() ResetFocus() void
ViewDoDisplay() void MyViewDoDisplay()
// do my render
23Overview of Patterns in GoF Design Patterns
- Creational
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Singleton
- Structural
- Adapter
- Bridge
- Composite
- Decorator
- Façade
- Flyweight
- Proxy
- Behavioral
- Interpreter
- Template Method
- Chain of Responsibility
- Command
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Visitor
24Design Pattern Benefits
- Adds to the language of OOP
- much information is expressed in a single term
- Documentation
- Reuse of ideas not just code
- CORBA problem - found a pattern on the web
- Anti-Patterns
25Problems
- Classification
- Tools
- Formalization
26Patterns on the web
- Patterns Home Page http//hillside.net/patterns
- http//st-www.cs.uiuc.edu/users/patterns/patterns.
html - http//c2.com/ppr/index.html - Portland Pattern
Repository - http//www.entract.com/bradapp/docs/patterns-intr
o.html
27Parting Quote
- It is possible to make buildings by stringing
together patterns, in a rather loose way. A
building make like this, is an assembly of
patterns. It is not dense. It is not profound.
But I is also possible to put patterns together
in such a way hat many patterns overlap in the
same physical space the building is very dense
it has many meanings captured in a small space
and through this density, it becomes profound. - Christopher Alexander