COM1204 Object-Oriented Design - Summer 2003 -- Design Pattern Examples in Java

Professor Futrelle

Version of 21 July 2003

7/21/2003: Note the addition of the Prototype Design Pattern at the end of this page.

The Design Pattern Java code samples here were developed by Professor Futrelle for this course. The complexity and quality of the code samples varies. See the notes below for details. Each directory referenced contains a file allSrc<patternName>.txt which has all the .java files concatenated together for easy perusal. Each directory contains the individual source and compiled files also.


Factory: A good example of a Factory Design Pattern is the Iterator class, which is also, of course, a Design Pattern in itself. Access the directory via this link. In this example two collections are implemented, one based on an underlying array and the other using a linked list. Both allow the construction of an iterator, which in turn supports hasNext() and next(), the methods defined in the MyIterator interface.

Another example of the Factory Design Pattern is the one given on the first Quiz in COM1204, Summer 2003. There were two extremely similar examples -- only the names were changed. The important point here is that getADog() or getACar() both returned an interface type, Barker and Speed, respectively, rather than the type of any concrete class. Here are the source and compiled files for the Dog example and here they are for the Car example. Here is a copy of Quiz #1A, corresponding to the Dog example.

Composite: The Composite Design Pattern (building hierarchies) is illustrated here by modeling a government organization (in some other galaxy, far, far away). There is only one class, Department, which can contain other Departments, and so forth. This example is very short. Access the directory via this link.

Observer: The most elaborate example built for the course by the end of June is for the Observer Design Pattern. Here is a separate page describing it here. The example models people talking and being heard and consists of four source files. In addition, it contains full Javadoc documentation and color-coded html versions of the source code with line numbers for easy reference.

Adapter: In this example, an existing "provider" class is adapted to server an earlier need. Access the directory via this link.

Singleton: This example is particularly interesting because it involves a simple internet-based "Date server". You run the server, then contact it via telnet. Each time you contact it, it updates the single class instance that keeps track of the number of contacts. Access the directory via this link.

Memento: This example allows a system to keep "mementos" to allow undoes. The undoes return the system to the most recent state that was saved. If this is continued the system is finally returned to the first saved state. Of course all this could be done behind the scenes so that every change made was saved so the system could be returned to any earlier state, all the way back to the initial state. Access the directory via this link.

UML diagrams for Memento: These can be found on this page.

Prototype: There is a page discussing this example as well as a page that has all the sources, color-coded with numbered lines referred to in discussion.

The tiny Melon class was written to emphasize how extremely easy it is to get started by designing a very simple example and writing the tiny bit of code required to implement it. This example does not exhibit any Design Pattern. Access the directory via this link.


Go to COM1204 home page.

Return to Prof. Futrelle's home page