Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

@Pattern(name="Composite", scope=Object, purpose=Structural, participants={"Component","Composite","Leaf","Client"})

Package dk.rode.thesis.composite

Implementations and examples of the Composite design pattern [Gamma95, p.163].


Interface Summary
CompositeSequence<E> A composite sequence is a sequence that represents an number of other contained sequences, possibly composite as well.

Class Summary
AbstractCompositeSequence<E> An abstract composite sequence implements the basic traits of the CompositeSequence interface.
CharSequenceCompositeSequence A char-sequence composite sequence is a composite sequence parameterised with the CharSequence type.
Main Composite tests.

Enum Summary
CompositePolicy Additional policies for formatting SequenceDecorator objects into char sequences (not part of the core Composite implementation).
CompositeStrategy Strategies for traversing composite sequence graphs.

Package dk.rode.thesis.composite Description

Implementations and examples of the Composite design pattern [Gamma95, p.163].


Here, the Composite participant is represented by the CompositeSequence class. It allows for Sequence objects to be composed into tree like structures and sequences thus represent both the Component (by inheriting the Sequence interface) and Leaf (contained in composites) participants.

The Client participant is the test application, i.e. the Main class.

UML Class Diagram:

Implementation notes:
This Composite implementation opts for type-safety over transparency as discussed by Gamma et al. [Gamma95, p.167-168]. Child management is defined in the Composite (CompositeSequence) participant because it can never make sense for Leaf components (Sequence). Unlike C++, the instanceof operator in Java provides a safe way to cast to a given type.

No explicit parent references are maintained for Sequence objects contained in a CompositeSequence. Sharing cannot generally be recommended because sequences carry state that is very likely to be manipulated through the Sequence interface, which CompositeSequence inherits. Explicit child ordering is possible by using a CompositeStrategy, such as CompositeStrategy.DEPTH_FIRST.

A limitation in the SequenceComposite interface is that each contained sequence must be declared to use or extend the same type parameter at construction time. However, it may be possible to use the Adapter pattern to circumvent this.

This package defines a basic abstract implementation of the CompositeSequence interface, namely the AbstractCompositeSequence class. It stores child sequences in a random-access list for fast traversal. Child-related operations are not thread-safe. The CharSequenceCompositeSequence class is an actual implementation of the CompositeSequence interface.

Gunni Rode / rode.dk

Gunni Rode / rode.dk

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.