The Composite pattern enables you to elegantly deal with recursive or hierarchical data structures. There are many examples of hierarchical data structures, making the Composite pattern very useful. A common example of such a data structure is one that you encounter every time you use a computer: the file system. The file system consists of directories and files. Every directory potentially has contents. The contents of a directory might be files, but they also might be directories. In this way, the file system of a computer is organized in a recursive structure. If you want to represent such a data structure programmatically, you can use the Composite pattern. The Composite pattern has the following elements:
|