The composite design pattern
The composite design pattern is used to describe groups of objects that should be treated the same way as a single one.
Note
The purpose of the composite design pattern is to compose objects into tree structures to represent whole-part hierarchies.
The composite design pattern is useful for removing code duplication and avoiding errors in cases where groups of objects are generally treated the same way. A popular example could be a filesystem in which we have directories, which can have other directories or files. Generally, the interface to interact with directories and files is the same, so they are good candidates for a composite design pattern.
Example class diagram
As we mentioned previously, filesystems are a good candidate for the composite design pattern. Essentially, they are just tree structures, so for our example, we will show you how to build a tree using the composite design pattern.
Consider the following class diagram:

As you can see from the preceding...