Java常用设计模式————组合模式

引言

组合模式,是一种类似递归算法的结构性设计模式,通过以简单的 List ,组合本类对象,实现树状对象结构的“部分、整体”的层次。

它可以让调用程序不需要关心复杂对象与简单对象的区别,而统一地实现处理逻辑。

对于个体与整体存在一定共性的数据场景的展示非常有帮助,比如省市区的树状结构,领导与员工、校院系等。

一、组合模式的类图结构

用套娃形容组合模式非常贴切,其编码核心其实就是用数组来组合下级对象,而下级对象本身也属于此类实例。一般就是用于描述某种极具共性的实体类,如省、市、区;领导、员工等。

二、组合模式的代码实现

package com.mht.demo.组合模式;

import java.util.ArrayList;
import java.util.List;

public class Employee {

	private String name;
	private String dept;
	private int salary;
	/** 通过数组组合自身对象*/
	private List<Employee> subordinates;

	public Employee(String name, String dept, int salary) {
		this.name = name;
		this.dept = dept;
		this.salary = salary;
		this.subordinates = new ArrayList<>();
	}

	public void add(Employee emp) {
		this.subordinates.add(emp);
	}

	public void remove(Employee emp) {
		this.subordinates.remove(emp);
	}

	public List<Employee> getSubordinates() {
		return subordinates;
	}

	@Override
	public String toString() {
		return "{\"name\":\"" + name + "\", \"dept\":\"" + dept + "\", \"salary\":\"" + salary + "\", \"subordinates\":"
				+ subordinates + "}";
	}
}

测试代码:

public class Client {
	public static void main(String[] args) {
		// 最上层员工,同时也是最高领导
		Employee ceo = new Employee("马云", "CEO", 30000);
		// 中层员工,同时也是中层领导
		Employee headSales = new Employee("Robert", "销售部", 20000);
		Employee headMarketing = new Employee("Michel", "市场部", 20000);
		// 基层员工,奋斗在业务一线
		Employee salesEmp = new Employee("Richard", "销售部", 10000);
		Employee salesEmp2 = new Employee("Rob", "销售部", 10000);
		Employee marketEmp = new Employee("Laura", "市场部", 10000);
		Employee marketEmp2 = new Employee("Bob", "市场部", 10000);


		ceo.add(headSales);
		ceo.add(headMarketing);

		headSales.add(salesEmp);
		headSales.add(salesEmp2);

		headMarketing.add(marketEmp);
		headMarketing.add(marketEmp2);

		// 打印CEO的管理层级
		System.out.println("CEO的管理层级:" + ceo);
		
		for (Employee headEmp : ceo.getSubordinates()) {
			System.out.println(headEmp);
			for (Employee subEmp : headEmp.getSubordinates()) {
				System.out.println(subEmp);
			}
		}
	}
}

测试结果:

CEO的管理层级:{"name":"马云", "dept":"CEO", "salary":"30000", "subordinates":[{"name":"Robert", "dept":"销售部", "salary":"20000", "subordinates":[{"name":"Richard", "dept":"销售部", "salary":"10000", "subordinates":[]}, {"name":"Rob", "dept":"销售部", "salary":"10000", "subordinates":[]}]}, {"name":"Michel", "dept":"市场部", "salary":"20000", "subordinates":[{"name":"Laura", "dept":"市场部", "salary":"10000", "subordinates":[]}, {"name":"Bob", "dept":"市场部", "salary":"10000", "subordinates":[]}]}]}
{"name":"Robert", "dept":"销售部", "salary":"20000", "subordinates":[{"name":"Richard", "dept":"销售部", "salary":"10000", "subordinates":[]}, {"name":"Rob", "dept":"销售部", "salary":"10000", "subordinates":[]}]}
{"name":"Richard", "dept":"销售部", "salary":"10000", "subordinates":[]}
{"name":"Rob", "dept":"销售部", "salary":"10000", "subordinates":[]}
{"name":"Michel", "dept":"市场部", "salary":"20000", "subordinates":[{"name":"Laura", "dept":"市场部", "salary":"10000", "subordinates":[]}, {"name":"Bob", "dept":"市场部", "salary":"10000", "subordinates":[]}]}
{"name":"Laura", "dept":"市场部", "salary":"10000", "subordinates":[]}
{"name":"Bob", "dept":"市场部", "salary":"10000", "subordinates":[]}

格式化后的结果:

{
    "name":"马云",
    "dept":"CEO",
    "salary":"30000",
    "subordinates":[
        {
            "name":"Robert",
            "dept":"销售部",
            "salary":"20000",
            "subordinates":[
                {
                    "name":"Richard",
                    "dept":"销售部",
                    "salary":"10000",
                    "subordinates":[

                    ]
                },
                {
                    "name":"Rob",
                    "dept":"销售部",
                    "salary":"10000",
                    "subordinates":[

                    ]
                }
            ]
        },
        {
            "name":"Michel",
            "dept":"市场部",
            "salary":"20000",
            "subordinates":[
                {
                    "name":"Laura",
                    "dept":"市场部",
                    "salary":"10000",
                    "subordinates":[

                    ]
                },
                {
                    "name":"Bob",
                    "dept":"市场部",
                    "salary":"10000",
                    "subordinates":[

                    ]
                }
            ]
        }
    ]
}

总结

组合模式的使用场景非常特定,一般就是在输出打印树状结构型数据时才会用到。

它模糊了简单元素和复杂元素的概念,客户端程序可以像处理简单元素一样来处理复杂元素,从而使客户端与复杂元素的内部结构解耦。

定义时,通过List组织自身对象。同时也并未用到抽象类或接口。

注意为数组提供必要的添加和移除方法。

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页