1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design...

35
1 Design Patterns Lecture 5 SD3043

Transcript of 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design...

Page 1: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

1

Design Patterns

Lecture 5 SD3043

Page 2: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

2

Outline

• Definition

• Design Patterns in Engineering

• Design patterns characteristic

• Types of Patterns

• Benefits of using Patterns

Page 3: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 4: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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)– ...

Page 5: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 6: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 7: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 8: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 9: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 10: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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.

Page 11: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 12: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 13: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 14: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

14

Creational Patterns: Singleton

• How does one ensure that only one instance of the company class is created?

Company

companyName

companyAddress

companyRegistrationNumber

getCompanyDetails()

Page 15: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 16: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 17: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 18: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 19: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 20: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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?

Page 21: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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?

Page 22: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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}

Page 23: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 24: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 25: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

25

Example – Decorator Pattern

• http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf

Page 26: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 27: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 28: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 29: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 30: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

• …

Page 31: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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)

Page 32: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 33: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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

Page 34: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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?

Page 35: 1 Design Patterns Lecture 5 SD3043. 2 Outline Definition Design Patterns in Engineering Design patterns characteristic Types of Patterns Benefits of using.

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