Lecture 3: Design Patterns

In which we discuss the concept of a design pattern and present some common patterns.

The Design Pattern

What is a design pattern?

According to architect Christopher Alexander:

According to Gamma, Johnson, Helm, and Vlissides (GJHV)

Between algorithm/data structure and complete system

Patterns: A Point of View

So what I told you was true, from a certain point of view....
- Obi-Wan Kenobi

Lines are not distinct

E.g., In C, class, encapsulation, and inheritance are all design patterns

Trees from last week: Pattern or application or type?

Categories of Patterns

You're probably most familiar with structural patterns, least familiar with creational patterns (even if you didn't call them that).


Putting objects together

Issues of composition

Distribution of quantity, containment


Controlling how objects act

Issues of delegation

Distribution of responsibility, action


Making objects

Guiding decision for everything else

Meta-distribution for all others

The Basic Patterns

Eight Common Patterns

The Structural Ones


AKA Wrapper

The Problem: Type Incompatibility

The Solution

The Trade-offs


The Problem: Dynamic grouping of "objects"

E.g., graphics program, draws lines, text, polygons, but lets you group them into more complex objects. Any can be selected, moved, copied, pasted, etc.

The solution: A common base class for "objects" and "collections"

The trade-offs


AKA Wrapper (!)

The Problem: Add behavior to many different objects, especially dynamically

The Solution: Pointer indirection and delegation

The Trade-offs

The Behavioral Ones


AKA Publish and Subscribe, Interest list, Notification list

The Problem: Many objects to be updated when one object changes

The solution: Watched object maintains list of interested objects

The trade-offs


AKA Policy

The Problem: Changeable preference for how to do something

The Solution: Pointer to an instance of a base-class; change pointer and object using the policy changes (observed) behavior

The trade-offs

Template Method

AKA Callbacks

The Problem: Common series of actions, but different situations require different intermediate activities

The Solution


The Creational Ones

Abstract Factory


The Problem: Common system, context-sensitive creation

E.g., Create user interface widgets based on "theme" or operating system - once created, application functions the same

The Solution


Factory Method

AKA Virtual Constructor

The Problem: Base class doesn't know what object to make, but derived class will know

Can't declare an instance variable if you don't know what its type is.

The Solution


And for Next Week.... (and the week after....)

Homework Assignment: Implement Your Graphical ls

Because the instructor is travelling next week, both this week's assignment and next week's assignment are being presented at this time.

Design it

Create a design document for the graphical version of /bin/ls whose specification you previously wrote. You will find the design patterns presented in class to be useful for simplifying the design as well as explaining the design.

This question is due Monday, September 30, 2002 by class time. This is a prose question. However, as the instructor will not be in class next Monday, please hand in your answer electronically. Email your answer to the instructor in either PDF or HTML format.

Implement it

From your design, create a functional, working application that meets your specification. You may use whatever programming language you desire, on whatever platform you desire.

This is a code question. Hand in a tarred and gziped file containing your complete code and documentation. The working code is due Monday, October 7, 2002