Browse Java Design Patterns & Enterprise Application Architecture

Composite Pattern

Use the Composite pattern in Java when clients should work with single objects and recursive groups through one common contract.

Composite is the structural pattern for part-whole hierarchies. In Java, it becomes useful when the client should not care whether it is dealing with one object or a tree of objects.

Typical examples include:

  • UI component trees
  • file or folder structures
  • product bundles
  • rule groups
  • scene graphs

Composite is powerful because it gives recursive structure a uniform API. It is dangerous because once the tree is public, ownership, mutation, and traversal rules must be designed carefully.

In this section

Revised on Thursday, April 23, 2026