page_title: Composite pattern
section_title: UML class and object diagram
hierarchical_section_title: Composite pattern / Structure / UML class and object diagram
caption_attribution_description: English: Class diagrams for the Composite design pattern. Design for uniformity versus design for type safety.
context_page_description: 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.
context_section_description: 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.
  Defining Child-Related Operations
  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()):
  Design for uniformity: Child-related operations are defined in the 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.
  Design for type safety: Child-related operations are defined only in the Composite class. Clients must treat Leaf and Composite objects differently. But type safety is gained because clients can not perform child-related operations on Leaf objects.
  The Composite design pattern emphasizes uniformity over type safety.
caption: Defining child-related operations in the Composite design pattern. [6]
exif: {}