From the course: Node.js: Design Patterns

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

The Composite pattern

The Composite pattern

- [Instructor] Sometimes, we want to perform the same operation on an individual element as we would a group of elements. Consider the grape.. I could tell you to eat a grape, and you'd know what to do with it. I could also tell you to eat these grapes, and so long as your hungry for grapes, you would then eat them all, one by one. In this example, the eat method can be used on a single grape, or a group of grapes. It will handle both cases accordingly. Composites work with trees, and when it comes to programming, trees are everywhere. One of the most common trees that we interact with is the file system, and the file system directories can be used to group files in other directories. The nodes that contain groups of other nodes are referred to as branches, and the file system, a file folder, represents a branch. If we traverse the tree, we will eventually end up with nodes that do not contain or group other…

Contents