Composite Pattern:
The Composite design pattern is a structural design pattern that allows us to treat individual objects and compositions of objects uniformly. It's called the Composite pattern because it composes objects into tree-like structures to represent part-whole hierarchies. These hierarchies can be used to model real-world systems where a component could be an individual object or an assembly of objects. The Composite design pattern consists of three components: 1. Component: This is the base interface, which defines the common methods. 2. Leaf: This class implements the Component interface and represents the objects the composition holds. It has no children. This is the end node of a coposite structure. 3. Composite: This class also implements the Component interface, but it can have children. It stores child components and implements child-related operations in the Component interface. It's used to store and manage the child components. The key idea of this pattern is that you don't need to care about whether the Component is a simple leaf or a complex coposite. You can treat all objects uniformly. Let's use a file system as an example. In a file system, a directory contains a set of files or other directories. Here, both files and directories can be seen as filesystem elements. In the example above, a "Directory" is composed of "FileSystemElement" objects, each of which could be either a "File" or another "Directory". When we call Print() on a directory, it calls print on all its "FileSystemElement" objects. Thus, we're treating a directory the same way as we'd treat an individual "FileSystemElement".