CompositePattern
COMP2511
Composite Pattern
Prepared by
Dr. Ashesh Mahidadia
Composite Pattern
These lecture notes use material from the wikipedia page at: https://en.wikipedia.org/wiki/Composite_pattern
and
the reference book “Head First Design Patterns”.
2COMP2511: Composite Pattern
Composite Pattern: Motivation and Intent
• In OO programming, a composite is an object designed as a composition of one-or-more similar
objects (exhibiting similar functionality).
• Aim is to be able to manipulate a single instance of the object just as we would manipulate a
group of them. For example,
• operation to resize a group of Shapes should be same as resizing a single Shape.
• calculating size of a file should be same as a directory.
• No discrimination between a Single (leaf) Vs a Composite (group) object.
• If we discriminate between a single object and a group of object,
code will become more complex and therefore, more error prone.
COMP2511: Composite Pattern 3
Composite Pattern: More Examples
A text document can be organized as part-whole hierarchy consisting of
• characters, pictures, lines, pages, etc. (parts) and
• lines, pages, document, etc. (wholes).
• Display a line, page or the entire document (consisting of many pages) uniformly using the same
operation/method.
COMP2511: Composite Pattern 4
Calculate the total price of an individual part or a complete subcomponent (consisting of many
parts) without having to treat part and subcomponent differently.
Chassis
Mainboard Disk
MemoryProcessor
subcomponent
Composite Pattern: Possible Solution
• Define a unified Component interface for both
Leaf (single / part ) objects and Composite (Group / whole) objects.
• A Composite stores a collection of children components (either Leaf and/or Composite
objects).
• Clients can ignore the differences between compositions of objects and individual objects, this
greatly simplifies clients of complex hierarchies and makes them easier to implement, change,
test, and reuse.
COMP2511: Composite Pattern 5
Composite Pattern: Possible Solution
• Tree structures are normally used to represent part-whole hierarchies. A multiway tree structure
stores a collection of say Components at each node (children below), to store Leaf objects
and Composite (subtree) objects.
• A Leaf object performs operations directly on the object.
• A Composite object performs operations on its children, and if required, collects return values
and derives the required answers.
COMP2511: Composite Pattern 6
Code Segment from the Composite class
Implementation Issue: Uniformity vs Type Safety
Two possible approaches to implement child-related operations
(methods like add, remove, getChild, etc.):
COMP2511: Composite Pattern 7
Design for Type Safety: only define child-related
operations in the Composite class.
Design for Uniformity: include all child-related
operations in the Component interface.See the next slide for more details.
Implementation Issue: Uniformity vs Type Safety
Design for Uniformity
• include all child-related operations in the Component interface, this means the Leaf class
needs to implement these methods with “do nothing” or “throw exception”.
• a client can treat both Leaf and Composite objects uniformly.
• we loose type safety because Leaf and Composite types are not cleanly separated.
• useful for dynamic structures where children types change dynamically (from Leaf to
Composite and vice versa), and a client needs to perform child-related operations regularly.
For example, a document editor application.
Design for Type Safety
• only define child-related operations in the Composite class
• the type system enforces type constraints, so a client cannot perform child-related
operations on a Leaf object.
• a client needs to treat Leaf and Composite objects differently.
• useful for static structures where a client doesn’t need to perform child-related operations
on “unknown” objects of type Component.
COMP2511: Composite Pattern 8
Composite Pattern: Demo Example
COMP2511: Composite Pattern 9
This example uses design for Uniformity (see composite.uniformity).
Sample code also includes design for Type Safety (see composite.typesafe).
COMP2511: Composite Pattern 10
Composite Pattern: Demo Example
Demos …
• Live Demos …
• Make sure you properly understand the demo example code available for this week.
COMP2511: Observer Pattern 11
Summary
• The Composite Pattern provides a structure to hold both individual objects and
composites.
• The Composite Pattern allows clients to treat composites and individual objects
uniformly.
• A Component is any object in a Composite structure. Components may be other
composites or leaf nodes.
• There are many design tradeoffs in implementing Composite. You need to balance
transparency/uniformity and type safety with your needs.
COMP2511: Composite Pattern 12
From the reference book: “Head First Design Pattern”