Java uses a composite pattern to implement a functional example of representing the organizational structure of a company
This article illustrates how Java uses the composite pattern to realize the function of representing the company's organizational structure. Share with you for your reference, as follows:
Model definition
Combination mode: combine objects into a tree structure to represent the hierarchical structure of "part as a whole". The combination mode makes users use single objects and combined objects consistently.
2、 Example of combination mode
1 mode analysis
The first mock exam is to illustrate this pattern by using the company's organizational chart.
After analysis, we get the static class diagram of the mode as follows:
2 code example
2.1 create employee abstract class
2.2 create Manager
2.3 creating ordinary employees
2.4 client test
The operation results are as follows:
3、 Design principle of this mode
1 unified treatment of individual objects and composite objects 2 Abstract oriented programming
4、 Use occasion
1 when you want to represent the "part as a whole" hierarchy of an object. 2. Users are expected to ignore the difference between composite objects and single objects. When users will use all objects in the composite structure uniformly.
The static class diagram of the composite pattern is as follows
For more information about Java algorithms, readers who are interested can see the topics on this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills