Some objects can essentially be defined recursively, as either a
primitive object or the composition of other objects.
The Composite Pattern defines an abstract base class that
specifies the behavior that needs to be exercised uniformly
across all primitive and composite objects.
While processing of primitive objects and composite objects is
handled differently, having to query which type of object is
being processed is undesirable.
At the heart of the Composite
Pattern is the ability for a client to
perform operations on an object
without needing to know whether it
has many other objects inside.
Chapter 4 – Page 76
Structural Pattern: Composite
The Client manipulates
objects in the composition
through the Component
interface.
The Component declares the
interface for the objects in the
composition, implements default
behavior for the interface that is
common for all classes, and
declares an interface for
accessing and managing the
child components.
The Composite defines behavior for components having children, stores
child components, and implements child-related operations in the
Component interface.
The Leaf, representing childless objects in the composition, defines
behavior for the primitive objects in the composition.
Chapter 4 – Page 77
The Composite Pattern
The Client makes use of
ArithmeticExpression
components.
Every operand in an
ArithmeticExpression is either a
NumericOperand leaf (like 3 or 17)
or a CompositeOperand composite
(like 8-5 or (20+31)/((5*3)-(2*6)).
Since each arithmetic operator here is binary, the component
forms a binary tree structure that represents the recursive
hierarchy of the ArithmeticExpression.
Chapter 4 – Page 78
Non-Software Composite Example
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Component
{
public:
virtual void traverse(string indent) = 0;
};
class Offspring: public Component
{
public:
Offspring(char val[]) { strcpy_s(value, val); }
void traverse(string indent) { cout << endl << indent << "Offspring: " << value; }
private:
char value[25];
};
Chapter 4 – Page 79
Composite Pattern C++ Code:
Family Tree
class Patriarch: public Composite
{
public:
Patriarch(char val[]): Composite(val){}
void traverse(string indent)
{
cout << endl << indent << "Patriarch: ";
Composite::traverse(indent);
}
};
class Son: public Composite
{
public:
Son(char val[]): Composite(val){}
void traverse(string indent)
{
cout << endl << indent << "Son: ";
Composite::traverse(indent);
}
};
Chapter 4 – Page 80
class Composite: public Component
{
public:
Composite(char val[]) { strcpy_s(value, val); }
void add(Component* c) { children.push_back(c); }
void traverse(string indent)
{
cout << value;
for (int i = 0; i < (int)children.size(); i++)
children[i]->traverse(indent + "\t");
}
protected:
vector<Component*> children;
char value[25];
};
Chapter 4 – Page 81
void main()
{
Patriarch first("Jock Ewing");
Son second("J.R. Ewing");
Son third("Gary Ewing");
Son fourth("Bobby Ewing");
Son fifth("Ray Krebbs");
first.add(&second);
first.add(&third);
first.add(&fourth);
first.add(&fifth);
second.add(&Offspring("John Ross Ewing III"));
second.add(&Offspring("Terrance Harper"));
second.add(&Offspring("James Beaumont"));
third.add(&Offspring("Lucy Ewing"));
fourth.add(&Offspring("Christopher Ewing"));
fifth.add(&Offspring("Lucas Krebbs"));
cout << "EWING FAMILY TREE" << endl;
first.traverse("");
cout << endl << endl;
}
• At the heart of the Composite Pattern is the ability for a
client to perform operations on an object without needing to
know that there are many objects inside.
• In addition to making the client code simpler, this pattern
makes it easier to add new kinds of components to a
hierarchical system.
• Possible applications include menus containing menu items
that are actually submenus, directories containing files that
are actually subdirectories, and row-column GUI layouts
containing widgets that are also row-column GUI layouts.
Chapter 4 – Page 82
Composite Pattern Advantages
© Copyright 2026 Paperzz