Combination pattern (Composite Pattern), also known as partial whole pattern, is used to treat a group of similar objects as a single object. The combination pattern combines objects according to the tree structure, which is used to represent the partial and overall hierarchy. This type of design pattern is a structural pattern, which creates a tree structure of groups of objects.
This pattern creates a class that contains its own group of objects. This class provides a way to modify the same group of objects.
We demonstrate the use of the combination pattern through the following example. An example demonstrates the hierarchical structure of employees in an organization. 意图: Objects are combined into a tree structure to represent a “part-whole” hierarchy. The combination mode enables users to use individual objects and composite objects consistently. 主要解决: In the problem of our tree structure, it blurs the concepts of simple elements and complex elements, and the client program can deal with complex elements like simple elements, thus decoupling the client program from the internal structure of complex elements. 何时使用: 1. You want to represent the part of the object-the overall hierarchy (tree structure). 2. You want the user to ignore the difference between the composite object and a single object, and the user will use all the objects in the composite structure uniformly. 如何解决: The branches and leaves achieve a unified interface, and the interface is combined within the branches. 关键代码: The interface is assembled inside the branch and contains the internal attribute List with Component in it. 应用实例: 1. Arithmetic expressions include operands, operators, and another Operand, where another Operand can also be an Operand, an operator, and another Operand. 2. In JAVA AWT and SWING, for Button and Checkbox, there are leaves, and Container is a branch. 优点: 1. The high-level module is easy to call. 2. Nodes increase freely. 缺点: When using the composite pattern, its leaves and branches are declared as implementation classes, not interfaces, violating the principle of dependency inversion. 使用场景: Partial, overall scene, such as tree menu, file, folder management. 注意事项: It is defined as a concrete class. We have a class Employee This class is treated as a composite model class CompositePatternDemo Class usage Employee Class to add a departmental hierarchy and print all employees. Create Employee Class with a Employee A list of objects. Use Employee Class to create and print a hierarchy of employees. Execute the program and the output is as follows: 6.11.1. Introduction ¶
6.11.2. Realize ¶
6.11.3. Step 1 ¶
Employee.java ¶
importjava.util.ArrayList;importjava.util.List;publicclassEmployee{privateStringname;privateStringdept;privateintsalary;privateList<Employee>subordinates;//构造函数publicEmployee(Stringname,Stringdept,intsal){this.name=name;this.dept=dept;this.salary=sal;subordinates=newArrayList<Employee>();}publicvoidadd(Employeee){subordinates.add(e);}publicvoidremove(Employeee){subordinates.remove(e);}publicList<Employee>getSubordinates(){returnsubordinates;}publicStringtoString(){return("Employee
:[ Name :"+name+", dept :"+dept+", salary :"+salary+"]");}}
6.11.4. Step 2 ¶
CompositePatternDemo.java ¶
publicclassCompositePatternDemo{publicstaticvoidmain(String[]args){EmployeeCEO=newEmployee("John","CEO",30000);EmployeeheadSales=newEmployee("Robert","Head
Sales",20000);EmployeeheadMarketing=newEmployee("Michel","Head
Marketing",20000);Employeeclerk1=newEmployee("Laura","Marketing",10000);Employeeclerk2=newEmployee("Bob","Marketing",10000);EmployeesalesExecutive1=newEmployee("Richard","Sales",10000);EmployeesalesExecutive2=newEmployee("Rob","Sales",10000);CEO.add(headSales);CEO.add(headMarketing);headSales.add(salesExecutive1);headSales.add(salesExecutive2);headMarketing.add(clerk1);headMarketing.add(clerk2);//打印该组织的所有员工System.out.println(CEO);for(EmployeeheadEmployee:CEO.getSubordinates()){System.out.println(headEmployee);for(Employeeemployee:headEmployee.getSubordinates()){System.out.println(employee);}}}}
6.11.5. Step 3 ¶
Employee :[ Name : John, dept : CEO, salary :30000 ]
Employee :[ Name : Robert, dept : Head Sales, salary :20000 ]
Employee :[ Name : Richard, dept : Sales, salary :10000 ]
Employee :[ Name : Rob, dept : Sales, salary :10000 ]
Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ]
Employee :[ Name : Laura, dept : Marketing, salary :10000 ]
Employee :[ Name : Bob, dept : Marketing, salary :10000 ]