Spring SALE
Composite

Composite in Swift

Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

Composite became a pretty popular solution for the most problems that require building a tree structure. Composite’s great feature is the ability to run methods recursively over the whole tree structure and sum up the results.

Complexity:

Popularity:

Usage examples: The Composite pattern is pretty common in Swift code. It’s often used to represent hierarchies of user interface components or the code that works with graphs.

Identification: If you have an object tree, and each object of a tree is a part of the same class hierarchy, this is most likely a composite. If methods of these classes delegate the work to child objects of the tree and do it via the base class/interface of the hierarchy, this is definitely a composite.

The following examples are available on Swift Playgrounds.
Kudos to Alejandro Mohamad for creating the Playground version.

Composite in Other Languages

Composite in C# Composite in C++ Composite in Go Composite in Java Composite in PHP Composite in Python Composite in Ruby Composite in Rust Composite in TypeScript