Design Patterns

advertisement
What Are Design Patterns and
Why Do I Need Them?
• Software professionals may be familiar with the
term "Design Patterns," but many have no idea
of where they come from and what they truly
are.
• Consequently, some do not see the value and
benefits design patterns bring to the software
development process, especially in the areas of
maintenance and code reuse.
• This talk will provide an introductory to design
patterns from a historical perspective.
Is This A New Idea?
• Town planners and architects talk of design
patterns all the time.
• Design patterns have their roots in the work of
Christopher Alexander an architect. When
talking about patterns in buildings and towns in
1977, he wrote
• “Each pattern describes a problem which
occurs over and over again in our environment
and then describes the core of the solution to
the problem, in such a way that you can use this
solution a million times over, without doing it the
same way twice.” Christopher Alexander,
• Shortly after, software professionals began to
incorporate Alexander’s principle into the
creation of early design pattern documentation
as a guide to novice developers.
• Design patterns gained popularity in computer
science after a certain book was published in
1994 by four authors, commonly know as Gang
of Four or GOF
Gang of Four
The GoF Book
• Gang of Four (GoF) Gamma, Helm,
Johnson, Vlissides, - founders of movement.
• Gamma et al, Design Patterns: Elements of
Reusable Object-Oriented Software, Addison
Wesley, 1995.
• They offer advice and help for developing quality
software
• An object-oriented design pattern systematically
names, explains and evaluates an important and
recurring design in object-oriented systems
Design Patterns Template
Term
Description
Pattern Name
Describes the essence of the pattern in a short, but expressive,
name
Intent
Describes what the pattern does
Also Known As
List any synonyms for the pattern
Motivation
Provides an example of a problem and how the pattern solves
that problem
Applicability
Lists the situations where the pattern is applicable
Structure
Set of diagrams of the classes and objects that depict the pattern
Participants
Describes the classes and objects that participate in the design
pattern and their responsibilities
Collaborations
Describes how the participants collaborate to carry out their
responsibilities
Consequences
Describes the forces that exist with the pattern and the benefits,
trade-offs, and the variable that is isolated by the pattern
• Different authors use different templates to describe their
patterns
• Information is not always presented in the same way.
Name
meaningful text that reflects the problem, e.g.
Bridge, Mediator, Flyweight
Problem addressed
intent of the pattern, objectives achieved within
certain constraints
Context
circumstances under which it can occur
Forces
constraints or issues that solution must address,
forces may conflict!
Solution
the static and dynamic relationships among the
pattern components. Structure, participants,
collaboration. Solution must resolve all forces!
•Consult your manual/source !!!
Types of Pattern
• There are 3 types of pattern …
– Creational: Concerned with the process of
object creation
– Structural: Concerned with the composition
of classes and objects
– Behavioral: Concerned with the ways in
which classes and objects interact and
distribute the work
Types of Design Patterns
Creational
Abstract
Factory
Builder
Factory
Prototype
Singleton
Structural
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Behavioural
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
Memento
Intent
Without violating encapsulation, capture and externalize an
object’s internal state so that the object can be restored to this
state later.
Applicability – use Memento when:
A snapshot of (some portion of) an object’s state must be saved so
that it can be restored to that state later, and
A direct interface to obtaining the state would expose
implementation details and break the object’s encapsulation.
Memento
Memento
Rect
Memento
x,y
x,y
Memento save()
void restore(Memento)
Memento save()
void restore(Memento)
RectCaretaker
Proxy
Intent
Provide a surrogate or placeholder for another object to control
access to it.
Applicability
You want a normally inaccessible or unavailable object to appear like
an ordinary object. For example, the object might be:
in a different process
in a disk archive
compressed
not computed yet
Forces
Turning an object into a Proxy, or vice versa, should be transparent
Proxy
Some Proxy Flavours!
Additional Behaviour Leads to Different Types of Proxies
A remote proxy provides a local representative for an object in a
different address space.
A virtual proxy creates expensive objects on demand. (lazy
loading)
A protection proxy controls access to the original object Protection
proxies are useful when objects should have different access
rights.
A cache proxy caches the output of the Subject class. A Proxy can
cache stable information about the subject to postpone accessing it.
Cached Proxy
CalculatorBase
Client
Calculate()
NumberMultiplierProxy
NumberMultiplier
Calculate()
Calculate()
Virtual Proxy
Imager
Client
GetImage()
ImageName
ImageProxy
QuickImage
GetImage()
ImageName
GetImage()
ImageName
FinalImage
GetImage()
ImageName
Participants of Proxy pattern
• Proxy
• Maintains a reference that lets the proxy access
the real object
• Provides an interface identical to the Subject’s
so that a proxy can be substituted for the real
subject.
• Controls access to the real subject and may be
responsible for creating and deleting it.
• Other responsibilities depend on the kind of
proxy
State
• Intent
– Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
• Applicability – use in either of the following cases:
– An object’s behavior depends on its state, and it must change its
behavior at run-time depending on that state.
– Operations have large, multipart conditional statements which
depends on the object’s state. This state is usually represented by one
or more enumerated constants. Often, several operations will contain
this same conditional structure. The State pattern puts each branch of
the conditional in a separate class. This lets you treat the object’s state
as an object in its own right that can vary independently from other
objects.
State Design Pattern
State
IStatus
Issue()
Pay()
Cancel()
Invoice
PendingState
Issue()
Pay()
Cancel()
PaidState
Issue()
Pay()
Cancel()
CancelledState
Issue()
Pay()
Cancel()
Summary
•
•
•
•
Design patterns assist developers by:
Providing templates for common problems
Creating a common nomenclature
Creating components that are more
reusable and flexible
• The .NET Framework includes features
that makes some of the patterns easier to
implement.
For more Information
• Websites
• Microsoft patterns & practices
• http://ww.microsoft.com/patterns
• http://www.dofactory.com/Patterns/Patterns.aspx
• Books
• Design patterns: Elements of Reusable Object Oriented
Software (Addison-Wesley, 1995)
• Head First Design Patterns - O’Reilly Media
Download