The composite pattern means including other objects within one object. These included objects may be terminal objects (no longer containing other objects), or they may be non-terminal objects (their interior still contains other objects, or they are called group objects). We call the objects nodes, that is, a root node contains many child nodes; some of these child nodes no longer contain child nodes, while others still contain child nodes, and so on. Clearly, this is a tree structure. The terminal nodes are called leaf nodes, the non-terminal nodes (group nodes) are called branch nodes, and the first node is called the root node. It is also similar to the structural form of a file directory: files can be called terminal nodes, and directories can be called non-terminal nodes (group nodes).
Ordinary Implementation
1. Let's first look at an ordinary implementation of a directory structure:
Directory node: Noder
File node: Filer
Test class: Clienter
Running result:
E:\ceshi\目录1 E:\ceshi\目录1\目录3 E:\ceshi\目录1\文件2.txt E:\ceshi\目录2 E:\ceshi\目录2\文件3.txt E:\ceshi\文件1.txt
2. Composite Pattern
From the above code, it can be seen that we defined the file node object and the directory node object separately. This is because the operations between files and directories are different: files have no lower-level nodes, while directories can have lower-level nodes. But can we think of it this way: since both files and directories can exist as lower-level nodes of a node, can we abstract both into one type of object? Although the operations of the two are different, we can define them concretely in the method implementations of the implementation classes. For example, a file has no method for adding a lower-level node, so we can throw an exception in this method of the file without implementing it concretely, while in the directory we concretely implement the add operation. Both have the display operation, and they can implement it separately. Moreover, since we abstract files and directories into one type, combined with polymorphism, we can implement it as follows:
Abstract class: Node
File implementation class: Filter
Directory implementation class: Noder
Test class: Clienter
Execution output result:
E://ceshi E:\ceshi\文件1.txt E:\ceshi\目录1 E:\ceshi\目录1\文件2.txt E:\ceshi\目录1\目录3 E:\ceshi\目录2 E:\ceshi\目录2\文件3.txt
From the above implementation, it can be seen that the so-called composite pattern actually refers to the problem of objects containing objects. It is laid out through composition (referencing objects inside objects). I think this composition is different from inheritance. Another meaning refers to the abstraction of child nodes in a tree structure (abstracting leaf nodes and branch nodes as child nodes), which is different from the ordinary way of separately defining leaf nodes and branch nodes.
3. Composite Pattern Application Scenarios
This composite pattern was born precisely for tree structures, so the usage scenario of the composite pattern is wherever tree structures appear. For example, operations on tree-structured data such as file directory display, multi-level directory presentation, and so on.
Original address: https://www.cnblogs.com/V1haoge/p/6489827.html