Chapter11 Composite
Chapter11 Composite
Contents
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
11.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
11.5.1 Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
11.5.2 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
11.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
11.1 Introduction
This lecture note will explain the structure of the composite design pattern. The com-
posite design pattern effectively builds a structure of related objects in the form of a tree.
These objects can be traversed by making using of the run-time polymorphic properties
of object oriented programming. To successfully implement the composite design pattern
design issues are considered and the clearing of the entire structure from memory after it
goes out of scope. In order to address these issues the C++ Standard Template Library
is introduced.
2
Vector List
Members
Category signature
Construction constructor y y
destructor y y
operator= y y
Accessor size y y
empty y y
front y y
back y y
operator[] y n
at y n
Mutator push front n y
pop front n y
push back y y
pop back y y
clear y y
Iterator begin
end
the back as well. A list is optmised for insertion, deletion and moving elements around in
the containers. Elements in a list are inserted or deleted either at the front or the back.
A summary of the members of the STL vector and STL list implementations are given
in table 1. The members are categorised in the following categories: Construction/De-
struction, Accessor, Mutator and Iterator. For each of these categories an indication of
whether a specific member is implemented for the container or not is given.
Other members also exist for each of the STL containers. Further details regarding the
containers are beyond the scope of this lecture note and can be found by searching the
internet. There are also good examples regarding the use of the containers available on
the internet.
Lists and vectors can easily be used by the caretaker of the Memento pattern in order
to store the state of more than one object. They can also be effective be applied to
the Abstract Factory example given in Chapter 7 so that all memory is cleared and the
example presented in the lecture notes does not exhibit a memory leak.
3
11.2.2 Anonymous objects
In order to understand the concepts better, some C++ background regarding anonymous
objects needs to be explained. An anonymous object is an object for which the code
creating the object has not got a handle to the object. Consider the following example:
Listing 1: Example of anonymous object creation
1 class A {
2 // a l l t h e c l a s s s t u f f
3 };
4
5 class B {
6 public :
7 B(A∗ i n ) { a = i n ; } ;
8 private :
9 A∗ a ;
10 };
11
12 // Some c l i e n t code
13 B b (new A ( ) ) ;
The object created and sent as a parameter to the constructor of class B is anonymous.
The client has no way of accessing this object and therefor when it goes out of scope the
object is not cleared from memory resulting in a memory leak.
The problem comes down to who takes ownership of the object. If the client keeps
ownership, then the client must be able to acquire a handle to the object in order to be
able to delete it. This can be achieved by defining a variable for the object a as follows
and replacing line 13 in listing 1 with the following code:
A∗ a = new A ( ) ;
B b(a ) ;
Another possibility for the client to gain ownership is if class B defines a getter operation
that returns a pointer to the object. For both these scenarios, it is now the obligation of
the main program to delete the object from the heap before the program goes out of scope.
As object b is on the stack, it will go out of scope when the main program terminates.
The following code will ensure that object a is delete from the heap.
delete a ;
Assuming that the design decision made for listing 1 is that ownership of the object is
given to class B. This means that when object b goes out of scope the object of class A
it points to must all be destroyed. Class B must therefore implement a destructor that
provides this functionality as the default destructor does not remove objects from the
heap memory by including the following code after line 7 of listing 1.
v i r t u a l ˜B( ) { delete a ; } ;
The destructor has been defined as virtual to ensure that if another class inherits from
B, the destructor is still called when that class goes out of scope.
4
11.3 Composite Design Pattern
11.3.1 Identification
Name Classification Strategy
Composite Structural Delegation (Object)
Intent
Compose objects into tree structures to represent part-whole hierarchies. Com-
posite lets clients treat individual objects and compositions of objects uniformly.
([2]:163)
11.3.2 Structure
11.3.3 Problem
Used in hierarchies where some objects are composite of other. Makes use of a store for
the children defined by Composite
11.3.4 Participants
Component
• provides the interface with which the client interacts.
Leaf
• do not have children, define the primitive objects of the composition.
Composite
• contains children that are either composites or leaves.
Client
• manipulates the objects that comprise the composite.
5
11.4 Composite Pattern Explained
The composite pattern inherently builds a tree (refer to Figure 2) with the intermedi-
ate nodes being instances of composite and the leaf nodes being instances of the leaf
participants.
Decorator
Used in conjunction with components to add state to the components. When a
decorator and a composite are combined, they usually share the same parent class.
Flyweight
Allows sharing of objects, particularly the leaf nodes
6
11.5 Example
11.5.1 Tree
In this example a tree will be built in which each node of the tree is represented by an
integer. The UML class diagram is given in Figure 3. The abstract Tree class defines
operations that can be applied to the tree. In this case a Tree node can be added to the
tree or the node can be printed.
7
Figure 5: Tree example with the composite taking ownership of destruction
c l a s s I n t e r m e d i a t e N o d e : public Tree
{
public :
I n t e r m e d i a t e N o d e ( int v ) ;
v i r t u a l void add ( Tree ∗ ) ;
v i r t u a l void p r i n t ( ) ;
virtual ˜ IntermediateNode ( ) ;
private :
int v a l u e ;
v e c t o r <Tree∗> next ;
};
I n t e r m e d i a t e N o d e : : I n t e r m e d i a t e N o d e ( int v ) : v a l u e ( v )
{
}
void I n t e r m e d i a t e N o d e : : p r i n t ( )
{
cout << ”−” << v a l u e << ” [ ” ;
v e c t o r <Tree ∗ >:: i t e r a t o r i t ;
8
}
cout << ” ] ” ;
}
IntermediateNode : : ˜ IntermediateNode ( )
{
v e c t o r <Tree ∗ >:: i t e r a t o r i t ;
11.5.2 Graphics
Consider the Shape Hierarchy that was introduced in the Abstract Factory chapter. Figure
6 serves as a refresher for this hierarchy.
From the hierarchy it is clear that the three (3) concrete triangle classes, the part of the
hierarchy from Parallelogram down, and Ellipse and its subclass Circle will form leaf nodes
of the Composite design pattern. The Shape class will fulfill the role of the Component
participant and a new class that represents the composite participant must be designed.
For this design, the area and perimeter of the composite will be determined by calculating
the sum of the area and perimeter of its constituents respectively. Figure 7 provides the
class structure for the composite shape hierarchy.
11.6 Exercises
1. Refactor the Tree example given in Figure 5 so that int value is defined in one
place only.
2. How would you modify the composite participant to build a binary tree? A binary
tree is a tree where the composite participant only has two children. These children
may either be an instance of composite or a leaf participant.
References
[1] cplusplus.com. STL Containers, 2011. URL http://www.cplusplus.com/stl/. [On-
line; accessed 31 August 2011].
[2] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design patterns :
elements of reusable object-oriented software. Addison-Wesley, Reading, Mass, 1995.
9
[3] Tomasz Müldner. C++ Programming with Design Patterns Revealed. Addison Wesley,
2002.
10
Figure 6: Shape hierarchy
11
Figure 7: Composite Shape hierarchy
12