Composite Pattern
Let’s try to understand the Composite pattern with an example of an operating system’s file system. In the file system, there are two types of objects: files and folders. There are cases when files and folders should be treated to be the same way. This is where the Composite pattern comes in handy.
Imagine that you need to run a search for a particular keyword in your file system. This search operation applies to both files and folders. For a file, it will just look into the contents of the file; for a folder, it will go through all files of that folder to find that keyword.
When to Use
- Composite Design pattern makes sense to use in cases when the composite and individual object needs to be treated in the same way from a client perspective.
– In our example above of the file system, let’s say search operation of a particular keyword needs to be executed. Now, this search operation applies to both File and Folder. For a File, it will just look into the contents of the file and for a Folder, it will go through all files in the hierarchy in that folder to find that keyword
- Use this pattern when the composite and individual object form a tree-like structure
– In our example, File and Folder do form a tree structure
Refer to https://swsmile.info/post/design-pattern-composite-pattern/ for Composite pattern.
...