1 design patterns lecture 5 sd3043. 2 outline definition design patterns in engineering design...
Post on 31-Dec-2015
223 Views
Preview:
TRANSCRIPT
1
Design Patterns
Lecture 5 SD3043
2
Outline
• Definition
• Design Patterns in Engineering
• Design patterns characteristic
• Types of Patterns
• Benefits of using Patterns
3
Patterns
• Are problem-centred, not solution-centred
• Are Discovered, not invented - they already exist
• Complement existing techniques and do not replace them
• Capture and communicate “best practice” and expertise
4
Application of Patterns
• Applied to software design since early 90’s
• Now used in in:– Project management – Organisation structures– Requirements analysis– System design– General modelling approaches– Programming – (called idioms)– ...
5
Origin of Patterns
• Christopher Alexander applied patterns to architecture in the mid-70s
• His book “A Pattern Language” is a catalogue of 253 patterns
• These document how to construct rooms, buildings and whole communities that people would like to live and work in
6
Software Pattern Definitions
• A pattern is proven solution to a problem that recurs in a particular context.
» A useful discussion of the possible definitions of a pattern can be found at http://hillside.net/patterns/definition.html
7
Patterns in engineering• How do other engineers find and use patterns?
– Mature engineering disciplines have handbooks describing successful solutions to known problems
– Automobile designers don't design cars from scratch using the laws of physics
– Instead, they reuse standard designs with successful track records, learning from experience
– Should software engineers make use of patterns? Why?• Developing software from scratch is also expensive
– Patterns support reuse of software architecture and design
8
The “gang of four” (GoF)
• Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides (Addison-Wesley, 1995)– Design Patterns book catalogs 23 different patterns as solutions to
different classes of problems, in C++ & Smalltalk– The problems and solutions are broadly applicable, used by many
people over many years– Example - Command Pattern http://www.oodesign.com/
– http://www.dofactory.com/Patterns/PatternCommand.aspx
– Why is it useful to learn about this pattern?• Patterns suggest opportunities for reuse in analysis, design and
programming
– http://sourcemaking.com/design_patterns
9
Example - Observer pattern
• Intent: – Define a one-to-many dependency between objects
so that when one object changes state, all its dependents are notified and updated automatically
• Used in Model-View-Controller framework– Model is problem domain– View is windowing system– Controller is mouse/keyboard control
• How can Observer pattern be used in other applications?
• JDK’s Abstract Window Toolkit (listeners)• Java’s Thread monitors, notify(), etc.• http://www.research.ibm.com/designpatterns/example.htm
10
Documenting Design Patterns -1• Pattern Name and Classification: A descriptive and
unique name that helps in identifying and referring to the pattern.
• Intent: A description of the goal behind the pattern and the reason for using it.
• Also Known As: Other names for the pattern. • Motivation (Forces): A scenario consisting of a problem
and a context in which this pattern can be used. • Applicability: Situations in which this pattern is usable;
the context for the pattern. • Structure: A graphical representation of the pattern.
Class diagrams and Interaction diagrams may be used for this purpose.
11
Documenting Design Patterns - 2
• Participants: A listing of the classes and objects used in the pattern and their roles in the design.
• Collaboration: A description of how classes and objects used in the pattern interact with each other.
• Consequences: A description of the results, side effects, and trade offs caused by using the pattern.
• Implementation: A description of an implementation of the pattern; the solution part of the pattern.
• Sample Code: An illustration of how the pattern can be used in a programming language
• Known Uses: Examples of real usages of the pattern. • Related Patterns: Other patterns that have some
relationship with the pattern; discussion of the differences between the pattern and similar patterns
12
Three Types of Patterns
• Creational patterns:– Deal with initializing and configuring classes and
objects
• Structural patterns:– Deal with decoupling interface and implementation of
classes and objects– Composition of classes or objects
• Behavioral patterns:– Deal with dynamic interactions among societies of
classes and objects– How they distribute responsibility
13
Creational Patterns
• Concerned with the construction of object instances
• Separate the operation of an application from how its objects are created
• Gives the designer considerable flexibility in configuring all aspects of object creation
14
Creational Patterns: Singleton
• How does one ensure that only one instance of the company class is created?
Company
companyName
companyAddress
companyRegistrationNumber
getCompanyDetails()
15
Creational Patterns: Singleton
• Solution – restrict access to the constructor!
Company
- companyInstance - companyName
- companyAddress
- companyRegistrationNumber
+ getCompanyInstance()
+ getCompanyDetails()
Class-scope (or static) attribute
(or static) operation
- Company() Private constructor
The use of class-scope operations allows global access
Class-scope
16
Creational Patterns: Singleton
Company
- companyInstance
- companyName
- companyAddress
+ getCompanyInstance()
+ getCompanyDetails()
- Company()
+ getCompanyDetails():String- UkCompany():UkCompany
- companyRegistrationNumber
UKCompany
+ getCompanyDetails():String- USACompany():USACompany
- companyRegistrationNumber
USACompany
+ getCompanyDetails():String- FrenchCompany():FrenchCompany
- companyRegistrationNumber
FrenchCompany
The attribute companyRegistrationNumber
has been moved to the subclasses where it is defined
differently in each.
The operation getCompanyDetails()
has been moved to the subclasses where it is
polymorphically redefined in each.
•Different subclasses of Company can be instantiated as needed, depending on run-time circumstances
17
Creational Patterns: Singleton
+ getInstance()
Singleton
- uniqueInstance
- singletonData
+ getSingletonData()
+ singletonOperation()
- Singleton()
Holds object identifier for the Singleton instance
Returns object identifier for the unique instance
Private constructor — only accessible via getInstance()
General form of Singleton pattern
18
Creational Patterns
• Abstract Factory:– Factory for building related objects
• Builder:– Factory for building complex objects incrementally
• Factory Method:– Method in a derived class creates associates
• Prototype:– Factory for cloning new instances from a prototype
• Singleton:– Factory for a singular (sole) instance
19
Structural Patterns
• Concerned with the way in which classes and objects are organized
• Offer effective ways of using object-oriented constructs such as inheritance, aggregation and composition to satisfy particular requirements
20
Structural Patterns: Composite
MediaClip
play()
VideoClip
play()
SoundClip
play()
• How can we present the same interface for a media clip whether it is composite or not?
21
Structural Patterns: Composite
Delegates to the play()
operation in the
components.
play() is
polymorphically
redefined
VideoClip
play()
SoundClip
play()
AdSequence
play()
addClip()
removeClip()
getChild()
* *
1
How can we incorporate composite structures?
22
Composite applied
Collection of MediaClip
object identifiers
for all m in mediaClipCollection m.play()
Delegates to the play() operation in
the components.
play() is polymorphically
redefined
AdSequence
mediaClipCollection
play()addClip()removeClip()getChild()changeSequence()
*
1
MediaClip
play()addClip()removeClip()getChild()
VideoClip
play()
SoundClip
play()
Advert {ordered}
23
Composite Pattern General Form
Collection of Component
object identifiers
for all c in componentCollection
c.anOperation()
anOperation() is polymorphically
redefined
Composite
componentCollection
anOperation()addComponent()removeComponent()getChild()
*
1
Component
anOperation()addComponent()removeComponent()getChild()
Leaf
anOperation()
OtherLeaf
anOperation()
Client
24
Structural patterns - example
• Describe ways to assemble objects to realize new functionality– Added flexibility inherent in object composition due to
ability to change composition at run-time– not possible with static class composition
• Example: Proxy– Proxy: acts as convenient surrogate or placeholder
for another object.• Remote Proxy: local representative for object in a
different address space• Virtual Proxy: represent large object that should be
loaded on demand• Protected Proxy: protect access to the original object
25
Example – Decorator Pattern
• http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf
26
Structural Patterns• Adapter:
– Translator adapts a server interface for a client• Bridge:
– Abstraction for binding one of many implementations• Composite:
– Structure for building recursive aggregations• Decorator:
– Decorator extends an object transparently• Facade:
– Simplifies the interface for a subsystem• Flyweight:
– Many fine-grained objects shared efficiently.• Proxy:
– One object approximates another
27
Behavioural Patterns
• Address the problems that arise when assigning responsibilities to classes and when designing algorithms
• Suggest particular static relationships between objects and classes and also describe how the objects communicate
28
Behavioral Patterns• Chain of Responsibility:
– Request delegated to the responsible service provider• Command:
– Request or Action is first-class object, hence re-storable• Iterator:
– Aggregate and access elements sequentially• Interpreter:
– Language interpreter for a small grammar• Mediator:
– Coordinates interactions between its associates• Memento:
– Snapshot captures and restores object states privately
29
Behavioral Patterns (cont.)
• Observer:– Dependents update automatically when subject changes
• State:– Object whose behavior depends on its state
• Strategy:– Abstraction for selecting one of many algorithms
• Template Method:– Algorithm with some steps supplied by a derived class
• Visitor:– Operations applied to elements of a heterogeneous object
structure
30
Patterns in software libraries
• AWT and Swing use Observer pattern• Iterator pattern in C++ template library &
JDK• Façade pattern used in many student-
oriented libraries to simplify more complicated libraries!
• Bridge and other patterns recurs in middleware for distributed computing frameworks
• …
31
More software patterns
• Design patterns – idioms (low level, C++): Jim Coplein, Scott Meyers
• I.e., when should you define a virtual destructor?– design (micro-architectures) [Gamma-GoF]– architectural (systems design): layers, reflection, broker
• Java Enterprise Design Patterns (distributed transactions and databases)
• Analysis patterns (recurring & reusable analysis models, from various domains, i.e., accounting, financial trading, health care)
• Process patterns (software process & organization)
32
Benefits of Design Patterns
• Design patterns enable large-scale reuse of software architectures and also help document systems
• Patterns explicitly capture expert knowledge and design tradeoffs and make it more widely available
• Patterns help improve developer communication• Pattern names form a common vocabulary
33
Before Using Patterns
• Before using a pattern to resolve the problem ask– Is there a pattern that addresses a similar
problem?– Does the pattern trigger an alternative solution that
may be more acceptable?– Is there a simpler solution? Patterns should not
be used just for the sake of it
34
Before Using Patterns
– Is the context of the pattern consistent with that of the problem?
– Are the consequences of using the pattern acceptable?
– Are constraints imposed by the software environment that would conflict with the use of the pattern?
35
Web Resources
• http://www.dofactory.com/• http://hillside.net/patterns/• Java Enterprise Design Patterns
http://java.sun.com/blueprints/patterns/catalog.html
• Yahoo Design Pattern Library http://developer.yahoo.com/ypatterns/
• Design Patterns http://www.vincehuston.org/dp/
• http://www.ida.liu.se/~uweas/Lectures/DesignPatterns01/home.html
top related