
CS 3230 Lab #File 6
Patterns, Polymorphism, Algorithms, and GUI 3
Design Patterns
The idea of a design pattern was first used by Christopher Alexander (The Timeless Way of
Building, 1979, New York: Oxford University Press), an architect, to describe a language of
building. Alexander’s ideas have had a great impact on computer science (see Vol. 1, p. 363 of
the course text book). Although there are now many books in print that describe useful design
patterns used to solve programming problems, one of the first, and still one of the best and most
influential, is Design Patterns: Elements of Reusable Object-Oriented Software by Gamma,
Helm, Johnson, & Vlissides (1997, Boston: Addison-Wesley).
Gamma, Helm, Johnson, & Vlissides (often referred to as the gang of four), describe 23 common
design patterns. Java utilizes two of their design patterns: factory method, and iterator. Java
also uses the model-view-controller design pattern first used in Smalltalk (see Vol. 1, pp. 364-
368). Alexander maintains that, “Each pattern describes a problem which occurs over and over
again in our environment, and 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 sam way twice” (p.
x). Gamma, Helm, Johnson, & Vlissides formalize design patterns, in the context of software
engineering by specifying four components of each pattern: (a) a pattern name, (b) the problem,
(c) the solution, and (d) the consequences of using the pattern (p. 3).
The Singleton Design Pattern
The singleton is a simple but nevertheless useful design pattern. According to Gamma, Helm,
Johnson, & Vlissides, it’s purpose is to “ensure a class has one instance, and provide a global
point of access to it” (p. 127). We will apply the singleton pattern to the Stratego class.
There are several ways of implementing a singleton, but only one is considered here (including a
simplification that is possible in the case of Stratego).
1. Define a static instance variable that points to the single instance of Stratego in the
program:
private static Stratego instance = null;
2. Make the constructor private (what does this accomplish and why is it important?)
3. Define a Stratego operation/method to create an instance of Stratego (but see step 5)
public void makeStratego()
{if (instance != null)
return;
instance = new Stratego();
}
Some programmers will combine this functionality with the operation describe in step 4.