8. design patterns

Post on 08-Dec-2014

629 views 2 download

Tags:

description

 

Transcript of 8. design patterns

Slide 1Object-Oriented Software Systems Engineering – Chapter 8

Design Patterns

Chapter 8

Slide 2Object-Oriented Software Systems Engineering – Chapter 8

Objectives

In this chapter we will: Discuss why software design patterns evolved Define and describe a software design pattern Describe fundamental patterns (GRASP patterns)

Slide 3Object-Oriented Software Systems Engineering – Chapter 8

Design Patterns – An Introduction

ObjectivesUnderstand why software design patterns evolvedDefine and describe a software design patternDescribe fundamental patterns (GRASP patterns)

Slide 4Object-Oriented Software Systems Engineering – Chapter 8

designing reusable software is hard novices are overwhelmed experts draw from experience some design solutions reoccur understanding reoccurring solutions is beneficial

in several ways

Why patterns?

Slide 5Object-Oriented Software Systems Engineering – Chapter 8

“Wisdom is often ascribed to those who can tell just the right story

at the right moment and who oftenhave a large number of stories to tell.”

Robert C. Shank

Why patterns?

Slide 6Object-Oriented Software Systems Engineering – Chapter 8

What is a pattern?

chess literature agriculture fishing architecture software design

from rules to expertise

oldest reference

wisdom vs. science

anecdotal documentation

pioneer work

Occurrence

Slide 7Object-Oriented Software Systems Engineering – Chapter 8

Architectural Patterns

Place at Window

Light from two sides

Deep terrace

Slide 8Object-Oriented Software Systems Engineering – Chapter 8

What is a pattern?

“Each pattern describes a problem which occursover and over again in our environment,

and then describes the core of the solution to that problem, in such a way that you can use

this solution a million times over, without ever doing it the same way twice.”

Alexander et al.

Slide 9Object-Oriented Software Systems Engineering – Chapter 8

“Aggressive disregard for originality” Rule of three

“Once is an event, twice is an incident, thrice it's a pattern.”

Jerry Weinberg

Patterns are proven

What is a pattern?

Slide 10Object-Oriented Software Systems Engineering – Chapter 8

The Origins of Design Patterns

They originate from the work of Christopher Alexander, a building architect in the 1970’s.

Alexander’s idea was to improve the quality of the buildings of the time by utilising proven ‘patterns’ of good architectural design.

‘Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem.’

Slide 11Object-Oriented Software Systems Engineering – Chapter 8

From Buildings to Software

Alexander’s work ‘discovered’ by Kent Beck and friends in the 1980’s.

They applied Alexander’s ideas about constructing buildings to the building of software.

Patterns are found in all areas of computing, from user interface design, the writing of device drivers to the use of databases.

Slide 12Object-Oriented Software Systems Engineering – Chapter 8

What is a software pattern?

proven software practice piece of literature building block possible abstraction levels:

language constructidiomdesign patternarchitectural pattern

Slide 13Object-Oriented Software Systems Engineering – Chapter 8

What is a Design Pattern?

A design pattern is defined as ‘ a description of communicating objects and classes that are customised to solve a general design problem in a particular context’.

Patterns capture good design principles and communicate them to others.

Design patterns represent the first legitimate attempt at design reusability.

Slide 14Object-Oriented Software Systems Engineering – Chapter 8

What is a software pattern?

“A methodology tells you how to write down the decisions you have made.

A pattern tells you which decisions to make, when and how to make them,

and why they are the right”

Beck & Johnson

Slide 15Object-Oriented Software Systems Engineering – Chapter 8

GRASP Patterns

General Responsibility Assigning Software Patterns

Five patterns Expert Creator High Cohesion Low coupling Controller

Slide 16Object-Oriented Software Systems Engineering – Chapter 8

ExpertAssign responsibility to the information expert

Expert

ProductSpecification

descriptionpriceUPC

Described-by

Contains

Sale

datetime

quantity

SalesLineItem

1..*

*

• What about responsibilities for What about responsibilities for • grandTotal of salegrandTotal of sale

• You also need subtotal for a line itemYou also need subtotal for a line item

• A method to return the priceA method to return the price

Slide 17Object-Oriented Software Systems Engineering – Chapter 8

Expert

ProductSpecification

descriptionpriceUPCgetPrice()

Described-by

Contains

Sale

datetimetotal sales()

quantitysubtotal()

SalesLineItem

1..*

*

BenefitsEncapsulation is maintained. Supports low coupling which leads to robust

and maintainable systems

Slide 18Object-Oriented Software Systems Engineering – Chapter 8

Creator Creator - Assign class B the responsibility to create an instance of class A if one of the following is true

B aggregates or contains A objects B records instances of A objects B closely uses A objects B has the intializing data that will be passed to A when created

Who should be responsible for creating a SalesLineItem instance?

ProductSpecification

descriptionpriceUPC

Described-by

Contains

Sale

datetime

quantity

SalesLineItem

1..*

*

Slide 19Object-Oriented Software Systems Engineering – Chapter 8

Low Coupling

p:Payment:POST

:Sale

makePayment() 1:create()

2:addPayment(p)

p:Payment

:POST :SalemakePayment() 1:makePayment(p)

1.1create()

Design 1

Design 2

Slide 20Object-Oriented Software Systems Engineering – Chapter 8

High Coupling

Payment

amountgetAmount()1

SaledateTimepaymenttotalSales()addPayment(p)makeLineItem(i,q)

saleendSale()enterItem(item,qty)makePayment(cash)

Post1

Captures

11 Paid-by

Design 1

Payment p = new Payment(cash);sale.addPayment(p);

sale.makeLineItem(item,qty)

Slide 21Object-Oriented Software Systems Engineering – Chapter 8

Low Coupling

Payment

amountgetAmount()1

SaledateTimepaymenttotalSales()makeLineItem(i,q)makePayment(cash)

saleendSale()enterItem(item,qty)makePayment(cash)

Post1

Captures

11 Paid-by

Design 2

Payment p = new Payment(cash)

sale.makeLineItem(item,qty)

sale.makePayment(cash)

Slide 22Object-Oriented Software Systems Engineering – Chapter 8

High Cohesion

Same example can be analysed from point of view of high cohesion (functional cohesion)

Design two is better

Slide 23Object-Oriented Software Systems Engineering – Chapter 8

Controller

Assign responsibility for handling a system event messages

Usually assigned to a controllerLogically the controller should not be a “window”,

“Frame”, “applet”, “application” e.t.c. these classes just handle the events and delegate them to a controller.

In practice view and control may be embedded in a single element known as a UI delegate (MVC architecture)

endSale(), enterItem() & makePayment() are embedded into the POST class

Slide 24Object-Oriented Software Systems Engineering – Chapter 8

Design Patterns: Essentials

Patterns are found through trial and error and by observation.

In general a design pattern has four essential elements:

The pattern nameThe problem the pattern is used to solveThe solution or template for implementing the patternThe consequences or results of applying the pattern.

Slide 25Object-Oriented Software Systems Engineering – Chapter 8

Patterns in the S/W Development Lifecycle

Design patterns are considered complementary to existing object-oriented methodologies.

Success in using design patterns largely depends in the correct selection of the appropriate pattern.

Knowledge and understanding of the use of existing documented patterns is all important.

Therefore the way patterns are catalogued must be unambiguous and complete.

Slide 26Object-Oriented Software Systems Engineering – Chapter 8

The ‘Gang of Four’

The most widely known work on design patterns is that of Gamma, Helm, Johnson and Vlissides . ‘The gang of four’ as they are commonly referred to.

Their book ‘Design Patterns: Elements of Reusable Object-Oriented Software’ was published in 1994.

It contains a description of the concepts of patterns, plus a catalog of 23 design patterns with their full documentation.

Slide 27Object-Oriented Software Systems Engineering – Chapter 8

Summary

In this chapter we have: Discussed why software design patterns evolved Defined and described a software design pattern Described fundamental patterns (GRASP patterns)