— the "Gang of Four" patterns implemented in Java 6
Master's Thesis, Computer Science
This is the homepage for my Master's Thesis Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6. Thesis keywords include Design Patterns; "Gang of Four"; Java 6; Object—Orientation (OO); and (impact of) Language Features. The thesis is written in UK English, and to tease your interest, the abstract and a few high—lights are presented below. The thesis is logically devided into two parts: 1) background and theory; and 2) implementation and evaluation. Emphasis is on the practical implementation and evaluation, which investigates how the "Gang of Four" patterns and Java 6 (can) interact.
The thesis and developed source code is freely available for download for those interested. The presentation given at the oral defence and UML class diagrams of the individual pattern implementations are also available for download. The thesis can also be browsed online, as well as the generated JavaDoc.
The picture to the right is me at the oral defence of the thesis given on November 2nd, 2007, at the Department of Computer Science, University of Copenhagen. Fabulous facilities, as you can see... :)
The thesis and source code are given a few small updates from time to time, e.g. because of errors or bugs, but note that no change—log/repository is kept! The topics covered in the thesis are by no means rocket science, and every programmer seems to have an opinion about OO and design patterns. You are - no doubt - no different. Therefore, feel free to drop a comment, sour grape, pad on the shoulder, question, report a bug or error, etc. However, I can promise that general and/or unconstructive bashing will be ignored! :) Click on my name below for email address, and put something meaningful like thesis or the thesis title in the subject to avoid my very aggressive spam filter. That is it. Enjoy & regards.
In this project, we perform an evaluation of the "Gang of Four" design patterns from a practical and experimental point of view using Java 6 as the implementation language. We investigate how Java 6 language features affect the application of the "Gang of Four" design patterns, individually and collectively. The investigation focuses on how the practical use of language features can affect the design pattern implementations, not how the features are constructed. To perform a reasonably structured and verifiable evaluation, we define a general evaluation approach on how to evaluate the "Gang of Four" patterns using a language as a catalyst. The premise is to implement all pattern functionality described in Implementation and Sample Code elements in the "Gang of Four" pattern descriptions, as these are the elements that primarily target the practical implementation, and evaluate the outcome.
Using the defined approach, we implement the "Gang of Four" patterns in Java 6 and investigate use of core language features (types, generics, closures, etc.), reflection (class literals, dynamic proxies, annotations, etc.), and special language mechanisms (synchronisation, serialization, cloning, etc.). The individual pattern evaluations show that with a few exceptions, all pattern functionality described in the Implementation and Sample Code elements, including Meta—information, can be implemented or simulated in Java 6 using the investigated features. The comparative evaluation shows that Java’s mixture of static and dynamic features are very well suited to express the "Gang of Four" pattern functionality. Creational and especially Behavioural patterns benefit from dynamic usage, while the static features make the implementations more robust, possibly reusable, and clarify pattern intent. The implementations furthermore provide novel, or at least alternative, approaches on how to implement Abstract Factory, Factory Method, Memento, Observer, Proxy, Singleton, and State in Java 6.
Section 9.3 in the thesis is dedicated to the identified thesis high—lights; they pretty much revolve around the patterns listed in the abstract above. In addition, a couple of small classes/components/ideas deserve special mention as well. Hence, I hope the following list will peak your interest:
- Singleton—as—Single—Constant Idiom
This idiom explains how a single enumeration constant can express the Singleton qualities and forces, for example as illustrated in the DanishAlphabetSequence implementation in this thesis.
Note: with the release of the second edition of Joshua Bloch's superb book "Effective Java", Bloch also describes Singleton implemented using enumerations and describes it as the "The Enum Approach" [Bloch08, p.18]. While this thesis was completed and published on the net well before the second edition of Bloch's book, I make no illusions about what this type of Singleton implementation will be referred to henceforth as already seen on Wikipedia as well as many Java related blogs. Also, since Bloch is one of the authors of the java.lang.Enum class, the idea — though unpublished — was probably coined in his mind long before my thesis was published. Still, I find the name Singleton—as—Single—Constant quite catchy and will continue to use it... Feel free to do so as well! :)
- Guarded Types
By using the functionality provided by the utility classes CallerClass and/or Caller, a guarded type enforces runtime protection of interface behaviour that has to be declared public in Java for compile—time type safety. This facilitates Singleton inheritance and at least the Memento implementation (and possibly State) as illustrated in the SimpsonsFamilySequence and GuardedSequenceMemento<E> classes, respectively.
- Type Literals
Type literals are used to enforce type safety for generic types and to act as reusable factories in Creational patterns such as Abstract Factory and Factory Method. Start by browsing the TypeLiteral<T> and Factory<T> classes, or perhaps the LoadableSingletonRegistry<T> class that aided by type literals allows for type—safe runtime loading of singleton types, even generic ones.
- Command Processor Variant
The Command Processor pattern is a variant of the Command pattern. This thesis illustrates a variant of the Command Processor pattern that may eliminate the need for Composite (macro) commands by letting executed commands spawn new commands to be executed by the Command Processor, for example in a depth—manner, including support for history lists and undo. The CommandProcessor class illustrates an implementation of this variant.
- Using Strategy for Meaningful String Representations
As Joshua Bloch notes in "Effective Java — Programming Language Guide", meaningful string representations of Java objects are very useful, at least for debugging and logging, and should thus in theory be implemented for each class. This is tedious and repetitive at best, and not very flexible. Instead, consider applying the Strategy pattern to format a given object into a specific textual representation as illustrated with the Stringable<T>, StringablePolicy<T> and ObjectPolicy types.
- Handle/Body Idiom
Java 6 implementation of the Handle/Body idiom, implemented using dynamic proxies. Look no further than the dk.rode.thesis.meta.reflect.proxy package and start with the Reference<T> interface.
- Reflection and Runtime—Retained Annotations
Combining these two features provide very flexible pattern implementations exhibiting behaviour that normally is ascribed to dynamic languages only. Creational and especially Behavioural patterns can benefit from the usage, for example Observer by annotating the notification methods without requiring a common observer interface. This is illustrated in the ObserverManager implementation.
- Dynamic Proxy Usage
Dynamic proxies are very useful in Structural patterns such as Proxy and Bridge, but also to simulate dynamic inheritance as illustrated in the State implementation in form of the StepSequence type and related classes.
- Utility Classes
The dk.rode.thesis.meta.util package contains various useful utility classes, such as the Throwables class used to manipulate throwables/exceptions and the Primitive enum used to test and convert to and from primitive types. The dk.rode.thesis.meta.reflect and dk.rode.thesis.meta.reflect.proxy packages furthermore contain various ready—to—use reflection utilities, such as the Reflection and ProxyFactory classes.
Compared to the pdf version, the HTML version is more interactive and links directly to the generated JavaDoc for source code references. It also contains additional indexes, such as a list of pattern references in the text, as well as a few extra paragraphs of text here and there.
The source code is fully documented using JavaDoc, including all non-public members. The generated documentation can be browsed online, can be downloaded, and is also referenced directly from the online version of the thesis.
The JavaDoc is sometimes quite verbose, especially package documentation, and discusses issues not necessarily covered in the thesis. It is recommended to keep it close while reading the thesis, in particularly the evaluation part starting from chapter 5.
The following files are freely available for download, but note the license terms for the source code:
- Thesis: thesis.pdf (~9.2 mb)
The final version of the thesis. The text is in UK English.
- Thesis Specification: thesis-specification.pdf (~140 kb)
The formal thesis specification ("work description") for the thesis. The text is in UK English.
- Thesis Defence: thesis-defence.pps (~2.3 mb)
The presentation given at the oral defence. The presentation is in UK English, but the notes are in Danish.
- Source Code: thesis-source-code.zip (~3.5 mb)
The complete Java 6 source code, fully documented including UML class diagrams. The source code compiles in Eclipse 3.3 using Java 6, or Java 5 with minor changes. Feel free to use the developed source code as you see fit, but note the license terms!
- UML Class Diagrams: thesis-uml-diagrams.zip (~7 mb)
Zip file containing all pattern and model UML class diagrams as png files at a resolution of 300 x 300 dots per inch.
- JavaDoc: thesis-javadoc.zip (~5.4 mb)
Generated JavaDoc, including all non-public members and UML class diagrams in low resolution. The documentation is in UK English.
- Eclipse Dictionary: thesis-dictionary.txt (~2 kb)
Spelling dictionary, which can be used in an Eclipse 3.3 project using the developed source code.
- Thesis, Thesis Specification, Thesis Defence, and Source Code: thesis-bundle.zip (~13.6 mb)
A single zip file containing the thesis, thesis specification, the presentation given at the oral defence, and the developed source code.
Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!
The original thesis and source code are available at rode.dk/thesis.