design patterns. patterns 1, 2, 3, … is a sequence that exhibits the pattern: the integers in...

125
Design Patterns

Upload: alanis-rosson

Post on 01-Apr-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Design Patterns

Page 2: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Patterns

• 1, 2, 3, …

is a sequence that exhibits the pattern:

The integers in their natural order

Page 3: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Another Sequence

• 1, 3, 5, …

– The pattern:

The odd integers, in order

Page 4: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

What is a Pattern?

• A template or model for imitation– A dress pattern

– A cookie cutter

– As we shall see, an object-oriented design diagram

• A set of rules– For the Fibonacci sequence, the rule is:

the ith element (except for f0 and f1) is equal to the sum of the (i-1)st element and the (i-2)nd element.

Page 5: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why are Patterns Useful?

• Patterns describing infinite sequences eliminate having to enumerate all values

0, 1, 1, 2, 3, 5, 8, …

Don’t have to list all the elements

Page 6: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why are Patterns Useful?

• A pattern can be a convenient membership test for a set of elements

– Regular expressions denote patterns of strings• For example

[a-zA-Z][a-zA-z0-9]*

is a pattern representing identifiers

– Compilers use regular expressions to test for identifiers

Page 7: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why are Patterns Useful?

• Knowledge of a pattern can enable one to easily produce new objects:

Page 8: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why are Patterns Useful?

• In particular, patterns discerned from existing situations may be applied to new situations

a, b, c, …

Monday, Tuesday, Wednesday, …

Page 9: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

But Sometimes the Similarities are not Obvious

Page 10: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Which Bring Us To…

• Do1, 2, 3, …

and

Monday, Tuesday, Wednesday, …

exhibit the same pattern?

Patterns and Abstraction

Page 11: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Patterns and Abstraction

1, 2, 3, …

the integers in their natural order

Monday, Tuesday, Wednesday, …

the days of the week in their natural order

Are these the same pattern?

Page 12: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Patterns and Abstraction

The pattern we really mean is

The elements of some sequence taken in their natural order

What about the fact that the integers are infinite and the days of the week are finite?

Page 13: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Some Patterns are Simple…ABC

Jackson 5Michael: you went to school to learn, girlThings you never, never knew before...……Jermaine: sit yourself down, take a seatAll you gotta do is repeat after me.

J5: a b cMichael: easy as...J5: 1 2 3Michael: or simple as...J5: do re miMichael: abc, 123, baby, you and me girl!

Page 14: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Others are Less Obvious…

• 2, 3, 5, 7, …– primes

• March, April, June, …– Months without a ‘Y’ in their names in natural order

Mensa likes to use patterns like these as part of their qualification test

Page 15: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Some are Quite Difficult…

• 6, 28, 496, …– perfect numbers

• e, t, a, …– most frequent letters in the English language in

descending order

Page 16: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

… And Some are Just Downright Ornery

• 214, 232, 234, … – Classrooms on 2nd floor of New Ingersoll from east to

west

• 1110, 2210, 3110, …– For cryin’ out loud– you’re CIS majors!

Especially if they’re not mathematical and you don’t know the context

Page 17: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Patterns Within Patterns

A B C

Page 18: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

And Now For Something Completely Different…

Summing Integers Read from a File

cin >> i;while (i >= 0) {total += i;cin >> i;

}

Page 19: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Finding the Maximum Double in a File

cin >> d;while (cin) {if (d > max) max = d;cin >> d;

}

Page 20: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Building a String from a Line of Characters

i = 0;c = getchar(); while (c != ‘\n’ && c != EOF)

s[i++] = c;c = getchar();

}s[i] = ‘\0’;

Page 21: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Three Individual Pieces of Code…

• Different datum types – int, double, char

• Different end-of-input conditions – negative datum, end-of-file, end-of-line/end-of-

file

• Different tasks:– summing, maximum, string construction

Page 22: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

… and Yet A Pattern Emerges

• In all three cases:– Values are read from a file– A condition is used to test for end-of-input– The values are processed in some fashion– A ‘priming-the-pump’ technique is used

Page 23: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

An Input Loop Pattern

read first itemwhile (still more items) {process itemread next item

}

Page 24: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why Are Such Patterns Useful?

• Avoids ‘reinventing the wheel’

• Avoids making the same mistakes over and over again

• Speeds up the development process

• ‘Reusability’ of sorts

Page 25: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Design Patterns

• Pattern concept applied to software design

• Not a finished product– Reuseability of design ‘ideas’

• Many patterns crop up over and over again

Page 26: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

A Non-Software Example- Flywheel

• A flywheel is a massive rotating disk

• The basic idea is to accelerate the flywheel to a very high speed and thus maintain the energy as rotational energy using the disk.

Page 27: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel - Samples

Page 28: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why a Flywheel?

• In the 70’s engineers were looking for a low-emissions, non-internal-combustion vehicle.

• Wanted to be able to ‘charge’ the vehicle and have it store that charge– Charging involved bringing the flywheel up to a high

speed

• Batteries were too bulky, heavy– Would need tens of batteries for a small vehicle

Page 29: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel – Useful For

• Storing kinetic energy – often in a fairly small space

• Maintaining a uniform force.

• Production of high power pulses

• Can also be used to create a form of gyroscopic effect

Page 30: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel – Advantages

• Not affected by temperature changes

• No limit to energy stored

• Simple to measure stored force (measure rotation speed)

Page 31: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel – Disadvantages

• Danger of explosive shattering of wheel

Page 32: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel – Parts

• Massive wheel

• Axle

• Bearings

Page 33: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel - Applications

• Low-cost toys (the kind you wind up by running the wheels along the floor)

• Energy-efficient cars (during braking, surplus energy is used to accelerate the flywheel which can subsequently power the driveshaft)

• Used on satellites to point the instruments in correct direction

• Potters wheel

Page 34: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel - Summary

• Note the variety of applications

• Yet all use the same basic design pattern

Page 35: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Flywheel - Summary

• Notice what we did here – Provided a motivational situation (low-emission

vehicle)

– Presented the purpose of the flywheel

– Described when to use one

– Presented the parts of the flywheel

– Discussed advantages and disadvantages

– Gave known applications

– Presented some samples

Page 36: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

A Simple Software Example

• You’ve got a program from CISC 3130 (Data Structures)

– Written in C++

– Uses a stack class template• Which you wrote (whole point of assignment)

• Massive application– Hundreds of modules

– Thousands of lines of code– Ok, Ok, two hundred lines of code in one file

– Stack usage scattered throughout system

Page 37: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Our Simple Software Example

• After CISC 3130, you learn about the STL (Standard Template Library)– Library of useful data structures, including those you learned in 3130

• You decide you want to play with it– Good to know for a tech interview

• So you toss out your stack and begin using the one from the STL

Page 38: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Problem

• Your stack’s operations:– push – places argument on top of stack– pop – pops stack returning value– isEmpty – true if empty

• STL’s stack’s operations:– push – same– peek – returns top of stack– pop – pops stack, no value returned– empty – different name, same semantics

There’s a mismatch in the interfaces

Page 39: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Solution #1

• Change the application code to conform to the new operations

Page 40: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Changes• Replace

s.isEmpty(); // yourswith

s.empty(); // STL’s

ands.pop(); // yours

withs.peek(); // STL’s

s.pop();

Global edit replace?

Page 41: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

??? !!!

#^%$!!!Scratch that solution!!

void clear() {

while(!s.isEmpty())

s.pop();

}

void clear() {

while(!s.empty())

s.peek();

s.pop();

}

Page 42: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

So??

• What’s Plan B?

Page 43: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Plan B• Add a new class, StackAdapter

– StackAdapter declares a member variable of type stack (from the STL).

– StackAdapter defines functions corresponding to the ones in your original stack class

– Some of the functions do nothing more than call corresponding functions of the STL stack

– Other functions act as adapters between the old and new semantics

Page 44: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The StackAdapter Classtemplate <class E>class StackAdapter {

public:push(E val) {s.push(val);}

E pop() {E x = s.peek();s.pop();return x;

}

bool isEmpty() {return s.empty();}

private:stack<E> s;

}

Page 45: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Adapter Pattern

• Plan B employs a design pattern known as Adapter

• The Adapter pattern

Converts the interface of a class into another interface clients expect. Adapter lets classes work together that otherwise couldn’t because of incompatible interfaces

Page 46: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Adapter Pattern Visually

StackAdapter

push()pop()

isEmpty()

Stack

push()pop()

empty()

s.isEmpty()

x = s. pop()s.pop();return x;

s

Page 47: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

You May Have Seen Something Similar• For example, when coding binary search…

– The recursive call for binary search is

bool binsrch(int a[], int lo, int hi)

… but the user wants to make the callbool binsearch(int a[], int n)

– We resolve this by adding an intermediate function:

bool binsearch(int a[], int n) {return binsrch(a, 0, n-1);

}

This is the a procedural analogy of the Adapter pattern; binsearch is usually called a wrapper function.

Page 48: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Design Patterns

• Introduced by architect Christopher Alexander (A

Pattern Language: Towns, Buildings, Construction) in the context of buildings and towns:

“Each pattern describes a problem which occurs over and over again in our environment, the describes the core of the solution to that problem, in such a way that you can use the solution a million times over, without ever doing it the same way twice.”

Page 49: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Architectural Design Patterns

• “A PLACE TO WAIT”– Bus stop

– Waiting room• adresses the common aspects of their design

• “ARCADES” - “covered walkways at edges of buildings, partly inside, partly outside”

http://architypes.net/patterns.php

Page 50: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘The Gang of Four’ Book• Introduced design patterns

to software design

• Much of this talk based upon this text

• In fact, it’s fair to say that one purpose of this talk is to provide a guide to how to read this text

• Bulk of text is a catalog of patterns

Page 51: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why Only ‘Object-Oriented’?

• Wouldn’t this have been useful before as well? – ‘Designing object-oriented software is hard, and designing

reusable objected-oriented software is even harder’ (Opening sentence of

‘Design Patterns’, Gamma, et al)

• The number and complexity of classes, objects and their interactions makes proper design a formidable task

– Also, might have been applicable before, but OOP (compared to say, procedural) ‘maxes’ out on reusability

• More opportunities for reuseable design• Everybody says that, but let’s see why

Page 52: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

OOP and Reusability

• So WHAT makes objected-oriented software more reusable than say applications designed and coded in a procedural style?– Classes?

– Inheritance

– Overloaded operators?

– Access control?

Page 53: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

• Allows one class to incorporate (reuse) another class’s implementation as part of its own

• All state (variables) and behavior (functions) of the existing (super/base/parent)class become part of the new (sub/child)class.

• Subclass can then add its own state/behavior

• The subclass is said to be a subtype of the superclass’ type

Not available in traditional procedural languages

Reusability Mechanisms – Inheritance

Page 54: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

class Counter {Counter() {val = 0;}void up(val++;}void down() {val--;}int get() {return val;}

int val;}

Reusability Mechanisms - Inheritance

class BoundedCounter extends Counter {

BoundedCounter(int m) {max = m;

}void up() {

if (val < max) val++;}

int max;}

Page 55: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

• Assembling or composing objects to get new functionality

• Basically one class contained as a variable of another

• Reusability comes from– The containing object (re)using the functionality of the contained

object(s)• … and thus avoiding implementing that behavior on its own

– Somewhat available in traditional procedural languages

Reusability Mechanisms - Composition

Page 56: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

class SSNum {boolean equals(SSNum other) {

…}…

}

Reusability Mechanisms - Composition

class Employee {boolean equals(Employee other) {

return id.equals(other.id);}…SSNum id;

}

Page 57: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

• One object carries out a request by delegating it to a second object (typically an instance variable of the first)

• Used widely in the context of composition, especially as a way of obtaining some of the flavor of inheritance

• In the previous example, the Employee object delegated the equality test to the (composed) Name object.

Somewhat available in traditional procedural languages

Reusability Mechanisms - Delegation

Page 58: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

• A set of function signatures (no bodies)

• Can be thought of as representing a type

• Can be specified in C++ via abstract classses and in Java via interfaces.

• A class (i.e., with function bodies) is said to implement the interface if the class defines (I.e., supplies bodies for) all the interface’s functions

– As with inheritance, the implementing class is also said to be a subtype of the interface’s type

Not available in traditional procedural languages

Reusability Mechanisms - Interfaces

Page 59: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

interface Collection {boolean add(Object obj);boolean remove(Object obj);int size();boolean isEmpty();

}

Reusability Mechanisms - Interfaces

Page 60: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Is-a Relationship

• An object of a subtype is compatible with the corresponding (parent) type.

• The object of the subtype is considered an object of the type as well

• This is known as the ‘is-a’ relationship…

• … and is the basis for much of the reusability of OOP

Page 61: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘Is-a’ in the Context of Inheritance

• An object of a subclass is compatible with the parent class’ type

– Thus given a Counter variable, a BoundedCounter object can be assigned to that variable:

BoundedCounter bctr;Counter ctr = bctr;

Page 62: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘Is-a’ in the Context of Interface

• An object of a class implementing an interface is compatible with the interface’s type

– Thus assuming Set implements the Collection interface. given a Collection variable, a Set object can be assigned to that variable:

Set set; Collection coll = set;

Page 63: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

So What???

• Given an interface, Collection, write a method, contains that returns true if a specified object belongs to a specified collection.

boolean contains(Collection coll, Object obj) {for(Object element : coll)

if (element.equals(obj)) return true;return false;

}

contains can accept (as the coll parameter) a Set, a Vector, in fact any class that implements the Collection interface.

Only one contains function need be written for a whole family of different (though related by the Collection interface) aggregate classes

Page 64: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

So What II???

• The similarity of classes conforming to an interface provides the opportunity for them to employ the same pattern/solution to a problem– Providing a solution in the form of an interface (i.e., a set of

methods) in turn provides a solution for ANY class that implements that interface.

– Implementing that interface means that the solution is available

– For example, the constructor that accepts a Collection can be used by all Collection classes since it employs only methods specified in the Collection interface and thus us available to all Collections!

Page 65: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Describing Design Patterns

• Recall our Flywheel

• In addition to presenting the flywheel we also presented– Motivation– Purpose– Application– Advantages/disadvantages

Page 66: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Describing Design Patterns

• Design Patterns are presented in a similar (fairly standardized) fashion

Page 67: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description - Name

• Name should be a good reflection of the pattern’s purpose

Adapter

Page 68: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description - Intent

• A statement answering:– What does the pattern do?

– What is its rationale?

– What problem does it address?

Adapter converts the interface of a class into another interface clients expect. Adapter lets classes work together that otherwise couldn’t because of incompatible interfaces

Page 69: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Also Known As

• Other names by which the pattern is known

Wrapper

Page 70: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Motivation

• A scenario that illustrates the problem and its solution via the classes and structures of the pattern

Our CISC 3130/STL Stack problem and solution

Page 71: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Applicability

• Under what circumstances can the pattern be applied?

• What are examples of poor designs that the pattern can address?

• How can such situations be recognized?

Use Adapter when:

– You want to use an existing class and its interface does not match the one you need

Page 72: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

• A graphical representation of the relationships among the classes/objects in the pattern

Pattern Description – Structure

Client Target

Request()

Adapter

Request() adaptee.specificRequest()

Adaptee

SpecificRequest()

adaptee

ClassInterfaceImplemented method()Abstract method()

method pseudo-code

Instance variable

subtype

Page 73: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Participants

• The classes and objects participating in the pattern

– Target (the interface consisting of push, pop, isEmpty)• Defines the specific interface that Client uses

– Client (Your 3130 program)• Interacts with objects conforming to the Target interface

– Adaptee (STL stack type)• Defines an existing interface that needs adapting

– Adapter (StackAdapter)• Adapts the interface of Adaptee to the Target interface

Page 74: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Collaborations

• How the participants interact to realize the pattern

– Clients call operations on an Adapter object• In turn the adapter calls Adaptee operations

Page 75: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Consequences

• What are the trade-offs and results of using the pattern?

– How much adapting does Adapter do?• Simple name changes all the way to supporting completely different

set of operations

There are several other consequences we won’t address here

Page 76: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Implementation

• Pitfalls, hints, techniques?

• Language-dependent issues?

Fairly straightforward

There are some language issues, but again, not for now

Page 77: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Sample code

We’ll just let our example be the sample code

Page 78: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Known uses

• Examples from real systems

Take a look at Gamma

Page 79: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Pattern Description – Related Patterns

• What other patterns are closely related to this one?

• What are the similarities? Differences?

• Which patterns use this pattern?

• Which patterns does this pattern use?

This was our first one!

Too early to supply answers for this

Page 80: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Why are Design Patterns Useful?• Avoids ‘reinventing the wheel’

• Avoids making the same mistakes over and over again

• Knowledge of a particular design pattern (like Adapter) is valuable…

• … but so is simply knowing about the concept of a design pattern• Knowing there are catalogs of patterns addressing design issues• Gets you thinking about design problems differently

Page 81: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Another Example

• Adapter was an example of a Structural design pattern– Structural patterns are concerned with how classes and

objects are composed to form larger structures.

• Our next example will present a Creational design pattern– Creational patterns help make a system independent of

how its objects are created and represented.

Page 82: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Another Example

• Us folks at – Provide interactive programming exercises

– Students submit solution code to our server which• Does operational and textual checks• Provides feedback

– In addition, there is a context-sensitive glossary/help system

• Generates hypertext links into the glossary, ‘on-the-fly’, for instructions and feedback

Page 83: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Creating a Glossary Object

Glossary glossary = new Glossary();

• Issues– Glossary is quite large – one is OK, but what if

there are tens (or hundreds) of concurrent users?

– No need for more than one glossary• It’s a query-only object

Page 84: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Creating a Glossary Object

• Is there a way to prevent more than one Glossary object from being created?

– The expression

glossary = new Glossary() creates a new instance each time

• And if we can restrict to one instance, how does the rest of the application access that single instance?– C++ could use a global variable (but how would we know where it

is?)– What about Java?

Page 85: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton Design Pattern - Intent

Ensure a class has only one instance and provide a global point of access to it

Page 86: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Motivation

• Just gave it to you, but since you asked– Ensuring single print spooler in a system– Ensuring a single buffer/node pool

Page 87: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Applicability

• Applicability: – Used when

• There must be exactly one instance, which must be accessible from a well-known point

Page 88: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton

static instance()

singletonOperation()

getSingletonData()

static uniqueInstance

singletonData

Singleton - Structure

return uniqueInstance

Page 89: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Participants

• Participants– Singleton

• Defines an instance operation that allows clients to access its unique instance– instance is a class (i.e., static) function/method

• May be responsible for creating its own unique instance

Page 90: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Collaborations

• Collaborations– Clients access a Singleton instance solely

through the instance operation

Page 91: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Consequences

• Controlled access to single instance• Reduced name space

– No global variables

• Can permit variable number of instances– I lied– it’s actually one glossary per language

glossary = new Glossary(“Java”)

Page 92: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton - Implementation

class Glossary {public static Glossary getGlossary() {

if (glossary == null)glossary = new Glossary();

return glossary;}

private Glossary();private Glossary glossary;

}

Note the private Glossary constructor

Page 93: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Singleton – ImplementationMultiple Instances

class Glossary {public static Glossary getGlossary(String language) {

Glossary glossary = map.get(language);if (glossary == null) {

glossary = new Glossary(language);map.put(language, glossary);

}return glossary;

}

private Glossary(String language);private Map<String, Glossary> = new HashMap<String, Glossary>;

}

Note the private Glossary constructor

Page 94: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Behavioral Patterns

• Structural and creational patterns are two of the three pattern purposes, the third being behavioral patterns

• Behavioral patterns are concerned with – algorithms – the assignment of responsibilities between objects– the patterns of communication between objects/classes– characterize complex control flow

• We’ll now present Observer, a behavioral pattern which is probably one of the most elegant patterns of all

Page 95: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

This is not Your Father’s Oldsmobile

• On a typical mid-to-high-end car these days– rain sensors turn on the wipers– wipers turn on the lights– shifting out of park turns on day running lights– turning on radio raises antenna– pressing brake disengages cruise control– and a host of other interactions between

sometimes seemingly unrelated components

Page 96: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

One Particular Set of Interacting Components

• Let’s focus on just three components– The interior light– The interior light switch

• Turning to ‘on’ turns on the interior light

– The car door• Opening turns on the interior light

Page 97: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Let’s Code a Car Class

class Car {…// Instance variablesDoor door = new Door();InteriorLightSwitch interiorLightSwitch = new InteriorLightSwitch();InteriorLight interiorLight = new InteriorLight();

}

Page 98: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The InteriorLight Class

class InteriorLight {

public boolean isOn() {return amOn;}void setOn(boolean b) {

if (amOn != b) {amOn = b;System.err.println("interior light turned “ + (amOn ? "on" : "off"));

}}boolean amOn = false;

}

Page 99: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The InteriorLightSwitch Class

class InteriorLightSwitch {

public boolean isOn() {return amOn;}void setOn(boolean b) {

if (amOn != b) {amOn = b;System.err.println("interior light switch “ + “moved to “ +

(amOn ? "on" : off"));interiorLight.setOn(amOn);

}}boolean amOn = false;

}

Page 100: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Door Class

class Door {

public boolean isOpen() {return amOpen;}void setOpen(boolean b) {

if (amOpen != b) {amOpen = b;System.err.println("door " + (amOpen ? "opened" : "closed"));interiorLight.setOn(amOpen);

}}boolean amOpen = false;

}

Page 101: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Mind Your Own Business

• Door knows it should turn on light• Interior switch knows it should turn on light• An alarm module (keyless entry) would also have to

turn on light

Who should know when to turn on the interior light?

Page 102: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

More Issues

• In ‘luxury’ model opening door causes seat to slide back– Now door must know to turn on light and slide seat

back

• But what about non-’luxury’ cars?– Separate door mechanism for luxury/non-luxury?

– Luxury/non-luxury models ‘wired’ differently?

Page 103: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘Spaghetti’ Responsibility Logic

• Turning on wiper switch– Must know to turn on wipers

– Wipers in turn must know to turn on headlights and activate 4WD sensor

– Headlights must know to dim radio display

Page 104: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘Spaghetti’ Responsibility Logic

• Pressing brake– Turns on ‘upper rear brake light’

– Turns on brake lights

– Disengages cruise control, but only if that option is present

– Initiates ALB sensor

Page 105: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

‘Spaghetti’ Responsibility Logic

• Every component must know about all components dependent upon it– Furthermore, every component becomes responsible for

those components

Page 106: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Still Not Convinced??

Well how about if I tell you that our implementation is wrong?

Page 107: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

A Sample Car interaction

public static void main(String [] args) {

Car car = new Car();car.door.setOpen(true); System.err.println(car);car.door.setOpen(false); System.err.println(car);car.interiorLightSwitch.setOn(true); System.err.println(car);car.door.setOpen(true); System.err.println(car);car.door.setOpen(false); System.err.println(car);

}

Page 108: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Output

door openedinterior light turned on (as a result of opening the door) door: opened / interior light: on / interior light switch: offdoor closedinterior light turned off (as a result of closing the door) door: closed / interior light: off / interior light switch: offinterior light switch moved to oninterior light turned on (as a result of turning the switch on) door: closed / interior light: on / interior light switch: ondoor opened (no light action -- light already on) door: opened / interior light: on / interior light switch: ondoor closedinterior light turned off (as a result of closing the door – but the

switch is still on!)

door: closed / interior light: off / interior light switch: on

Page 109: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

OOP = Responsibility-Driven Programming

• Goal is for objects (components) to be responsible for themselves

• ‘Decoupling’ objects simplifies the design

• The simpler, more self-responsible objects are easier to reuse

Page 110: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Observer Design Pattern - Intent

Define a (many to one) dependency between objects so that when one object changes state, all its

dependents are notified and updated automatically

Page 111: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Observer - Applicability

• Use Observer when either– A change to one object requires changing others, and

you don’t know how many others need to be changed

– An object should be able to notify other objects without making assumptions about who those objects are (minimize coupling between the objects)

Page 112: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Observer - Structure

from Wikipedia

Page 113: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Observer Interface

interface Observer {void notify();

}

• An Observer’s notify method is called (by the Subject object) when the Subject object has changed.

Page 114: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Observable Superclass

class Subject {void registerObserver(Observer observer) {

observers.add(observer);}

void notifyObservers() {for (observer : observers)

observer.notify();}

Set<Observer> observers = new HashSet<Observer>();}

Page 115: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The InteriorLight Classclass InteriorLight implements Observer {

InteriorLight(InteriorLightSwitch interiorLightSwitch, Door door) {this.interiorLightSwitch = interiorLightSwitch; this.door = door;interiorLightSwitch.registerObserver(this);door.registerObserver(this);

}

public boolean isOn() {return amOn;}private void setOn(boolean b) {

if (amOn != b) {amOn = b;System.err.println("interior light turned " + (amOn ? "on" : "off"));

}}

public void notify() {setOn(interiorLightSwitch.isOn() || door.isOpen());

}

boolean amOn = false;InteriorLightSwitch interiorLightSwitch;Door door;

}

Page 116: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The InteriorLightSwitch Class

class InteriorLightSwitch extends Subject {public boolean isOn() {return amOn;}void setOn(boolean b) {

if (amOn != b) {amOn = b;System.err.println("interior light switch “ + “moved to " +

(amOn ? "on" : "off"));notifyObservers();

}}boolean amOn = false;

}

Page 117: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Door Class

class Door extends Subject {

public boolean isOpen() {return amOpen;}void setOpen(boolean b) {

if (amOpen != b) {amOpen = b;System.err.println("door " + (amOpen ? "opened" : "closed"));notifyObservers();

}}

boolean amOpen = false;}

Page 118: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

The Output This Time

door openedinterior light turned on door: opened / interior light: on / interior light switch: offdoor closedinterior light turned off door: closed / interior light: off / interior light switch: offinterior light switch moved to oninterior light turned on door: closed / interior light: on / interior light switch: ondoor opened door: opened / interior light: on / interior light switch: ondoor closed door: closed / interior light: on / interior light switch: oninterior light switch moved to offinterior light turned off door: closed / interior light: off / interior light switch: off

Page 119: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Minimizing Hard-coding

• The CodeLab engine can check exercises for any language that has a compiler

• An appropriate set of tools and entities– compilers, linkers, compiler message analyzers, glossaries – must be created specific to the language

• We want this done without hard-coding any knowledge of particular languages into the engine

• This is accomplished using the Factory Method pattern

Page 120: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Maintaining Consistency

• Furthermore, the language-specific tools used in an exercise must be consistent with each other (i.e., be restricted to tools of that specific language).

• This is addressed using the Abstract Factory pattern

• This design was introduced into the engine by Josh Goldsmith as part of his 88.1 project.

Page 121: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Invoking the Tools

• Many of the tools used to build and test exercises form tree-like hierarchies of sorts

Java Tool

Java Compiler Tool

Java Interpreter Tool

C++ Tool

C++ Compiler Tool

C++ Linker Tool

Executable Tool

• A Java Tool is executed by executing a Java Compiler Tool followed by executing a Java Interpreter Tool

• Similarly for the C++ Tool

Page 122: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Treating Individual Objects and Compositions Identically

• Sometimes a full Java Tool is launched

• Other times simply the Java Compiler Tool

• We want to launch and subsequently process both tools (the composite Java Tool and the atomic Java Compiler Tool) identically

• This is achieved using the Composite pattern

Page 123: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Filtering Streams

• When processing submissions and exercise output, we often want to– Remove whitespace completely

– Remove a final trailing linefeed

– Compress multiple whitespace to a single whitespace

– Remove comments

– Ignore case

• This is done using the Strategy pattern

Page 124: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Iteration

• Any class implementing the Collection interface must supply a uniform means of iterating over its elements.

• This is done via the Iterator pattern

Page 125: Design Patterns. Patterns 1, 2, 3, … is a sequence that exhibits the pattern: The integers in their natural order

Whew!

• In summary– Design patterns provide highly flexible, reusable

solutions to commonly arising design situations

– Patterns are recognized as valuable repositories of information based upon analysis and experience

– Catalogs exist enumerating collections of patterns

– Conscious use of patterns is widespread