This example describes the use of combination mode to implement the function of representing the organizational structure of a company. Share it for your reference, as follows:
1. Pattern definition
Combination mode: Combining objects into a tree structure to represent a "partially whole" hierarchy. Combination mode makes users consistent with the use of individual objects and combined objects.
2. Examples of combination modes
1 Pattern Analysis
We borrow the company's organizational chart to illustrate this model.
After analysis, we obtain the static class diagram of this pattern as follows:
2 Code Examples
2.1 Establish employee abstract classes
package com.demo.composite;/** * Employee class interface* * @author * */public abstract class Staff { // Employee number protected String no; // Employee name protected String name; // Position protected String position; // Salary protected float salary; // Private property length string private int length; // Construct method public Staff(String no, String name, String position, float salary) { this.no = no; this.name = name; this.position = position; this.salary = salary; // Calculate the total byte length this.length += (no == null || "".equals(no.trim())) ? 0 : no.getBytes().length; this.length += (name == null || "".equals(name.trim())) ? 0 : name .getBytes().length; this.length += (position == null || "".equals(position.trim())) ? 0 : position.getBytes().length; this.length += String.valueOf(salary).getBytes().length; } // Get basic user information public void printUserBaseInfo() { System.out.println("|" + this.no + " " + this.name + " " + this.position + " " + this.salary); } // Add employee information public abstract void add(Staff staff); // Delete employee public abstract Staff remove(String no); // Print employee information public abstract void printEmployeesInfo(int layer); // Print several characters protected void printChar(int layer) { for (int j = 0; j < layer * 2; j++) { System.out.print("-"); } } // Private method prints a line protected void printLine() { System.out.print("+"); for (int i = 0; i < this.length + 4; i++) { System.out.print("-"); } System.out.println("-"); } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; }}2.2 Create a manager
package com.demo.composite.sub;import java.util.ArrayList;import com.demo.composite.Staff;/** * Manager (someone with other employees under his command) * * @author * */public class Manager extends Staff { // Store information about employees private final ArrayList<Staff> arrayList = new ArrayList<Staff>(); // Construct method public Manager(String no, String name, String position, float salary) { super(no, name, position, salary); } /** * Add an employee*/ @Override public void add(Staff staff) { this.arrayList.add(staff); } /** * Delete employee information*/ @Override public Staff remove(String no) { Staff staff = null; if (no != null && !"".equals(no.trim())) { for (int i = 0; i < this.arrayList.size(); i++) { if (this.arrayList.get(i) == null) { continue; } if (no.equals(this.arrayList.get(i).getNo())) { staff = this.arrayList.remove(i); break; } } } return staff; } /** * Print employee information*/ @Override public void printEmployeesInfo(int layer) { int tmpLayer = ++layer; for (int i = 0; i < this.arrayList.size(); i++) { if (this.arrayList.get(i) == null) { continue; } // Print '-' printChar(tmpLayer); // Print basic employee information this.arrayList.get(i).printUserBaseInfo(); // Print employee information under your post this.arrayList.get(i).printEmployeesInfo(tmpLayer); } }}2.3 Create ordinary employees
package com.demo.composite.sub;import com.demo.composite.Staff;/** * Ordinary employees (really working) * * @author * */public class Employees extends Staff{ // Construct method public Employees(String no, String name, String position, float salary) { super(no, name, position, salary); } /** * Add employee information*/ @Override public void add(Staff staff) { return; } /** * Delete employee information*/ @Override public Staff remove(String no) { // Return null directly return null; } /** * Print employee information*/ @Override public void printEmployeesInfo(int layer) { return; }}2.4 Client Testing
package com.demo;import com.demo.composite.Staff;import com.demo.composite.sub.Employees;import com.demo.composite.sub.Manager;/** * Main application* * @author * */public class Client{ /** * @param args */ public static void main(String[] args) { // Company CEO Staff boss = new Manager("1", "Big Boss", "CEO", 100000); /** * CEO has several department managers*/ // Finance Department Manager Staff financeManager = new Manager("11", "President Zhang", "Financial Department Manager", 60000); // Staff personnel Manager = new Manager("12", "President Wang", "President Manager", 60000); // Staff technicalManager = new Manager("13", "President Chen", "Technical Department Manager", 60000); /** * There are also assistants and several supervisors in the technical department*/ // Staff deptAssistant = new Manager("1301", "Assistant Wang", "Department Assistant", 20000); // Technical Department Manager1 Staff deptManager1 = new Manager("1302", "Supervisor1", "Technical Supervisor", 30000); /** * Technical Supervisor DeptManager1 There are software engineers (the person who works in the end) */ Staff softwareEngineer1 = new Employees("1302001", "Zhang San", "Software Engineer", 5000); Staff softwareEngineer2 = new Employees("1302002", "Li Si", "Software Engineer", 5500); Staff softwareEngineer3 = new Employees("1302003", "Wang Wu", "Software Engineer", 4500); // Add employee information for technical director 1 deptManager1.add(softwareEngineer1); deptManager1.add(softwareEngineer2); deptManager1.add(softwareEngineer3); // Technical Department Director 2 Staff deptManager2 = new Manager("1303", "Supervisor2", "Technical Director", 30000); // Add to technical department manager: Department assistant, Technical Director 1 and Technical Director 2 technicalManager.add(deptAssistant); technicalManager.add(deptManager1); technicalManager.add(deptManager2); // Marketing Manager Staff marketingManager = new Manager("14", "Mr. Wu", "Market Manager", 60000); // Add to CEO: Finance Department Manager, Human Resources Department Manager, Technical Department Manager and Marketing Manager boss.add(financeManager); boss.add(personnelManager); boss.add(technicalManager); boss.add(marketingManager); // Print CEO information boss.printUserBaseInfo(); // Print CEO employee information boss.printEmployeesInfo(1); }}The operation results are as follows:
|1 Big Boss CEO 100000.0
---|11 General Zhang Finance Department Manager 60000.0
---|12 General Wang Human Resources Department Manager 60000.0
---|13 General Chen Technical Department Manager 60000.0
------|1301 Assistant Wang Department Assistant 20000.0
-----|1302 Supervisor 1 Technical Supervisor 30000.0
-------|1302001 Zhang San Software Engineer 5000.0
-------|1302002 Li Si Software Engineer 5500.0
-------|1302003 Wang Wu Software Engineer 4500.0
------|1303 Supervisor 2 Technical Supervisor 30000.0
---|14 General Manager Wu Marketing Department 60000.0
3. The design principles of this model
1 Treat individual and combined objects uniformly
2 Abstract oriented programming
4. Use occasions
1 When you want to represent the "partial and whole" hierarchy of the object.
2 When the user wants to ignore the difference between a combined object and a single object, the user will use all objects in the combined structure uniformly.
The static class diagram of the combination mode is as follows
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.