Composite pattern

1

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

Overview

The Composite design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

Problems the Composite design pattern can solve

When defining (1) objects and (2) objects that act as containers for objects, clients must treat them separately, which complicates client code.

Solutions the Composite design pattern describes

This enables clients to work through the interface to treat and objects uniformly: objects perform a request directly, and objects forward the request to their child components recursively downwards the tree structure. This makes client classes easier to implement, change, test, and reuse. See also the UML class and object diagram below.

Motivation

When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, more error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "has-a" relationship between objects. The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.

When to use

Composite should be used when clients ignore the difference between compositions of objects and individual objects. If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous.

Structure

UML class and object diagram

In the above UML class diagram, the class doesn't refer to the and classes directly (separately). Instead, the refers to the common interface and can treat and uniformly. The class has no children and implements the interface directly. The class maintains a container of child objects and forwards requests to these. The object collaboration diagram shows the run-time interactions: In this example, the object sends a request to the top-level object (of type ) in the tree structure. The request is forwarded to (performed on) all child objects ( and objects) downwards the tree structure. There are two design variants for defining and implementing child-related operations like adding/removing a child component to/from the container and accessing a child component : The Composite design pattern emphasizes uniformity over type safety.

UML class diagram

Variation

As it is described in Design Patterns, the pattern also involves including the child-manipulation methods in the main Component interface, not just the Composite subclass. More recent descriptions sometimes omit these methods.

Example

This C++14 implementation is based on the pre C++98 implementation in the book. The program output is

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

View original