Quickly understand composite patterns in Java design patterns
Composite pattern is a common design pattern (but I think it is a little complex). It is also called composite pattern, sometimes called part whole pattern. It is mainly used to describe the relationship between part and whole.
Personal understanding: combination mode is to assemble parts into a whole.
It is defined as follows:
The objects are combined into a tree structure to represent the "part whole" hierarchy, so that users have consistency in the use of single objects and combined objects.
The general class diagram is as follows:
Roles included in the composite pattern:
● component abstract component role defines the common methods and attributes of the objects participating in the composition, and some default behaviors or attributes can be defined.
● leaf component leaf object, under which there are no other branches, that is, the smallest unit of traversal.
● composite branch component is a branch object. Its function is to combine branch nodes and leaf nodes to form a tree structure.
Common source code:
Branch Construction
A leaf node is an object without child objects. It defines the behavior of the original object participating in the combination.
The test class is responsible for the establishment of the tree structure, and can traverse the whole tree recursively.
Advantages of combined mode:
● the call of high-level module is simple. All nodes in a tree mechanism are components. There is no difference between local and overall for the caller, that is, the high-level module does not have to care whether it is dealing with a single object or the whole composite structure, which simplifies the code of high-level module. ● nodes can be added freely. After using the combination mode, we can see whether it is easy to add a branch node or a leaf node. As long as we find its parent node, it is very easy to expand, conforms to the opening and closing principle, and is very beneficial to future maintenance.
Disadvantages of combination mode:
Composite mode has a very obvious disadvantage. See our definition in the scene class, and mention the definition when using leaves and branches? Directly used the implementation class! This is very inappropriate in interface oriented programming and conflicts with the dependency inversion principle. Readers should consider it clearly when using it. It limits the influence scope of your interface.
Usage scenarios: ● scenarios for maintaining and displaying partial overall relationships, such as tree menu, file and folder management. ● scenarios that can separate some modules or functions from a whole.
matters needing attention:
As long as it is a tree structure, it is necessary to consider using the combination mode. This must be remembered. As long as it is necessary to reflect the relationship between the part and the whole, and this relationship may be deep, consider the combination mode.
summary
The above is all about quickly understanding the composite pattern in Java design pattern. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java design pattern - detailed explanation of factory design pattern, visitor pattern use scenarios and code examples of Java design pattern, abstract factory code examples of Java design pattern notes, etc. if you have any questions, you can leave a message at any time, and the editor will reply to you in time. Thank you for your support!