The composite pattern
This pattern is very often used to manipulate a group of objects. Swift, like many other languages already makes use of the composite pattern in its internal structure. For example, in the case of the UIView
class available in the cocoa
framework, which defines a common behavior of an app layout. Then, individuals view objects in the view hierarchy can be leaf nodes (such as labels) or composites that have collections of other views (such as table view controllers).
Roles
This pattern permits you to treat single components and a group of components in the same way by providing a structured hierarchy of objects. It allows you to build structures of objects in the form of trees that contain both compositions of objects and individual objects as nodes.
Using this pattern, we can create complex trees and treat them as a whole or as parts. Operations can be applied to the whole or the parts too.
We generally find the add
, remove
, display
, find
, and group
operations in the...