DELAPARRA
Index
Color
NL
Category: Design patterns

Composite

Explained by Updated

What is Composite?

Composite: Compose objects into tree structures to represent part–whole hierarchies, letting clients treat individual objects and compositions uniformly. The Composite pattern lets you treat a group of objects the same way as a single object. A tree of items, where each item can contain other items, is handled through one shared interface.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Composite

How is Composite used in practice?

Think of a menu with submenus, a folder with files and subfolders, or a product bundle that contains products and smaller bundles. Asking the top item for its price or size works the same whether it is one product or a deeply nested bundle.

What should you keep in mind with Composite?

In code, every node implements the same method, such as getPrice(). A leaf returns its own value; a composite adds up the values of its children. The calling code does not need to know which kind it has. This removes many if checks and makes recursive structures much easier to work with.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with C

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.