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.[1]
The Composite [2] 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.
When defining (1) Part
objects and (2) Whole
objects that act as containers for Part
objects, clients must treat them separately, which complicates client code.
Component
interface for both part (Leaf
) objects and whole (Composite
) objects.Leaf
objects implement the Component
interface directly, and Composite
objects forward requests to their child components.This enables clients to work through the Component
interface to treat Leaf
and Composite
objects uniformly:
Leaf
objects perform a request directly,
and Composite
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.
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.[4] 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.
Composite should be used when clients ignore the difference between compositions of objects and individual objects.[1] 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.
In the above UML class diagram, the Client
class doesn't refer to the Leaf
and Composite
classes directly (separately).
Instead, the Client
refers to the common Component
interface and can treat Leaf
and Composite
uniformly.
The Leaf
class has no children and implements the Component
interface directly.
The Composite
class maintains a container of child
Component
objects (children
) and forwards requests
to these children
(for each child in children: child.operation()
).
The object collaboration diagram
shows the run-time interactions: In this example, the Client
object sends a request to the top-level Composite
object (of type Component
) in the tree structure.
The request is forwarded to (performed on) all child Component
objects
(Leaf
and Composite
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 (add(child)/remove(child)
) and accessing a child component (getChild()
):
Component
interface. This enables clients to treat Leaf
and Composite
objects uniformly. But type safety is lost because clients can perform child-related operations on Leaf
objects.Composite
class. Clients must treat Leaf
and Composite
objects differently. But type safety is gained because clients cannot perform child-related operations on Leaf
objects.The Composite design pattern emphasizes uniformity over type safety.
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.[7]
The following example, written in Java, implements a graphic class, which can be either an ellipse or a composition of several graphics. Every graphic can be printed. In Backus-Naur form,
Graphic ::= ellipse | GraphicList GraphicList ::= empty | Graphic GraphicList
It could be extended to implement several other shapes (rectangle, etc.) and methods (translate, etc.).
import java.util.ArrayList;
/** "Component" */
interface Graphic {
//Prints the graphic.
public void print();
}
/** "Composite" */
class CompositeGraphic implements Graphic {
//Collection of child graphics.
private final List<Graphic> childGraphics = new ArrayList<>();
//Adds the graphic to the composition.
public void add(Graphic graphic) {
childGraphics.add(graphic);
}
// This is only shallowCopy
public void add(List<Graphic> compositGraphic) {
childGraphics.addAll(compositGraphic);
}
//Prints the graphic.
@Override
public void print() {
for (Graphic graphic : childGraphics) {
graphic.print(); //Delegation
}
}
}
/** "Leaf" */
class Ellipse implements Graphic {
//Prints the graphic.
@Override
public void print() {
System.out.println("Ellipse");
}
}
/** Client */
class CompositeDemo {
public static void main(String[] args) {
//Initialize four ellipses
Ellipse ellipse1 = new Ellipse();
Ellipse ellipse2 = new Ellipse();
Ellipse ellipse3 = new Ellipse();
Ellipse ellipse4 = new Ellipse();
//Creates two composites containing the ellipses
CompositeGraphic compositGraphic2 = new CompositeGraphic();
compositGraphic2.add(ellipse1);
compositGraphic2.add(ellipse2);
compositGraphic2.add(ellipse3);
CompositeGraphic compositGraphic3 = new CompositeGraphic();
compositGraphic3.add(ellipse4);
//Create another graphics that contains two graphics
CompositeGraphic compositGraphic = new CompositeGraphic();
compositGraphic.add(compositGraphic2);
compositGraphic.add(compositGraphic3);
//Prints the complete graphic (Four times the string "Ellipse").
compositGraphic.print();
}
}
By: Wikipedia.org
Edited: 2021-06-18 19:28:58
Source: Wikipedia.org