Pattern Clinic

Purpose

A good programmer should know at least a dozen common design patterns well.

He or she should constantly be on the lookout for opportunities to use these patterns.

Review

Recognizing Common Design Problems

The skill of using design patterns boils down to the following steps:

1. Identify the problem.

2. Identify the pattern that solves the problem.

3. Substitute specific classes and interfaces for the roles in the pattern's class diagram.

4. Faithfully translate to code.

Every pattern solves a common design problem. For example, the Adapter Pattern solves the following problem:

A class exists that has the desired functionality, but does not implement the desired interface.

Patterns as Plays

A pattern is like a play. There are actors, roles, relationships, and a script.

For example, the Adapter Pattern is a play involving four roles: Client, Target, Adapter, and Adaptee. The Adaptee is the role played by the existing class that has the desired functionality, the target is the interface required by the client. The adapter implements the target interface by forwarding to the adaptee.

The relationships between the roles are expressed using a class diagram:

Although we are using a class diagram, Client, Target, Adapter, and Adaptee are not necessarily classes. They are roles. The programmer will replace these roles by actual classes and interfaces.

A sequence diagram expresses the interaction between instances of these roles; it is similar to the script of a play. It shows the messages exchanged and the sequence of exchanges:

A play can have many performances. A performance consists of actors playing the roles and following the script.

A performance of a design pattern—called an instance of the pattern-- consists of actors (classes and interfaces) that play the roles in the pattern. For example, the following diagram shows two "performances" of the Adapter Pattern. In both performances the role of client is played by the Application class and the role of Target is played by the Platform interface. In one performance the role of adapter is played by the class UNIXAdapter while the role of adaptee is played by the UNIX class. In the other performance MSWindowsAdapter plays the adapter role while the MSWindows class plays the adaptee role:

The Adapter Pattern

The Problem:

A class exists that has the desired functionality, but does not implement the desired interface.

The Roles:

Client, Target, Adapter, Adaptee

Casting Call:

Fill in the following table from the scenarios below:

Scenario #

Client

Target

Adaptee

1

 

 

 

2

 

 

 

3

 

 

 

4

 

 

 

5

 

 

 

Describe the Adapter you would provide using Java pseudo-code.

Scenarios

1. A computer requires 110 volts of power. Unfortunately, outlets in Europe are 220 volts.

2. An experiment requires a thermometer that measures temperatures in degrees centigrade. A thermometer is available that measures temperatures in degrees Fahrenheit.

3. A graphics program needs a class to implement the Circle interface. An existing library has an Ellipse class.

The Strategy Pattern

The Problem:

Different instances of the same class need to use different implementations of the same method. And/Or, a single instance of a class needs to dynamically change the implementation of one of its methods.

The Roles:

Context, Abstract Strategy, Concrete Strategies

Casting Call:

Fill in the following table from the scenarios below:

Scenario #

Context

Abstract Strategy

Concrete Strategies

1

 

 

 

2

 

 

 

3

 

 

 

4

 

 

 

5

 

 

 

 

Scenarios:

1. Some customers want a bartender to shake their martinis, others like them stirred.

2. A fighter needs to adjust his fighting style depending on his opponent. Sometimes it's best to kick, other times wrestle, and other times retreat.

3. A parent sometimes needs to be strict and other times flexible.

4. A chess player needs to be aggressive against some opponents and cautious against others.

5. Sometimes a soldier needs to attack, other times retreat, and still other times to flank.

6. A football player needs to change the play at the last minute.

The Composite Pattern

The Problem:

A container (or assembly) needs to contain primitive components and sub-containers (or sub-assemblies).

The Roles:

Abstract Component, Composite Component, Primitive Components

Casting Call:

Fill in the following table from the scenarios below:

Scenario #

Abstract Component

Composite Component

Primitive Components

1

 

 

 

2

 

 

 

3

 

 

 

4

 

 

 

5

 

 

 

 

Scenarios:

1. An officer is a soldier who commands soldiers. Privates are soldiers who don't get to command anyone.

2. A circuit is made from gates and sub-circuits.

3. A directory contains sub-directories and files.

4. A test suite contains sub-suites and test cases.

5. A panel contains sub-panels and controls.

6. An expression contains sub-expressions, numbers, and variables.

The Publisher-Subscriber Pattern

The Problem:

An object needs to notify other objects when it changes state. But the number and types of these other objects is unknown and can vary dynamically.

The Roles:

General Publisher, Specific Publisher, Abstract Subscriber, Concrete Subscribers

Casting Call:

Fill in the following table from the scenarios below:

Scenario #

Concrete Publisher

Concrete Subscribers

1

 

 

2

 

 

3

 

 

4

 

 

5

 

 

Scenarios:

1. All monsters in a dungeon need to know when a hero enters.

2. Bidders at an auction need to know when the auctioneer announces a new price.

3. All applications need to shutdown when the computer shuts down.

4. The owner of a FaceBook account needs to be notified when someone posts a message on his wall.

5. The owners of a Google Doc need to be notified when someone updates it.

The Actor-Role Pattern

The Problem:

The same person (or organization) can play many roles in an enterprise. This can lead to replications of personal data such as address and phone number. This in turn can lead to inconsistencies if some but not all occurances of this personal data is updated.

The Roles

Actor, Role

Casting Call:

Fill in the following table from the scenarios below:

Scenario #

Actor

Roles

1

 

 

2

 

 

3

 

 

4

 

 

5

 

 

 

Scenarios

1. A movie star plays a character.

2. A corporation buys a property.

3. A man is a husband, son, and a father.

4. A person is a student and employee

5. Classes are strategies or adapters.