设计模式之15组合模式(笔记)

定义:

1.1 定义:Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。)

1.2 通用类图:[透明模式]

1.3 通用代码:

public abstract class Component {
	// 个体和整体都具有的共享
	public void doSomething() {
		// 编写业务逻辑
	}

	// 增加一个叶子构件或树枝构件
	public abstract void add(Component component);

	// 删除一个叶子构件或树枝构件
	public abstract void remove(Component component);

	// 获得分支下的所有叶子构件和树枝构件
	public abstract ArrayList<Component> getChildren();
}

public class Leaf extends Component {
	@Deprecated
	public void add(Component component) throws UnsupportedOperationException {
		// 空实现,直接抛弃一个“不支持请求”异常
		throw new UnsupportedOperationException();
	}

	@Deprecated
	public void remove(Component component)
			throws UnsupportedOperationException {
		// 空实现
		throw new UnsupportedOperationException();
	}

	@Deprecated
	public ArrayList<Component> getChildren()
			throws UnsupportedOperationException {
		// 空实现
		throw new UnsupportedOperationException();
	}

	public void doSomething() {
		System.out.println("我是一片叶!");
	}
}

public class Composite extends Component {
	// 构件容器
	private ArrayList<Component> componentArrayList = new ArrayList<Component>();

	// 增加一个叶子构件或树枝构件
	public void add(Component component) {
		this.componentArrayList.add(component);
	}

	// 删除一个叶子构件或树枝构件
	public void remove(Component component) {
		this.componentArrayList.remove(component);
	}

	// 获得分支下的所有叶子构件和树枝构件
	public ArrayList<Component> getChildren() {
		return this.componentArrayList;
	}

	public void doSomething() {
		System.out.println("我是一树枝!");
	}
}

public class Client {
	public static void main(String[] args) {
		// 创建一个根节点
		Component root = new Composite();
		// 创建一个树枝构件
		Component branch = new Composite();
		// 创建一个叶子节点
		Component leaf = new Leaf();
		// 建立整体
		root.add(branch);
		branch.add(leaf);
		display(root);
	}

	// 通过递归遍历树
	public static void display(Component root) {
		root.doSomething();
		for (Component c : root.getChildren()) {
			if (c instanceof Leaf) { // 叶子节点/
				c.doSomething();
			} else { // 树枝节点
				display(c);
			}
		}
	}
}


优点

2.1 高层模块调用简单:树形机构中所有节点都是Component,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码;

2.2 节点自由增加:容易扩展,符合OCP,对维护有利。

缺点

3.1 在上述遍历树时,需要判断是叶子还是树枝,这点若直接通过类型判断,则会形成“对实现类的依赖”与依赖倒置原则冲突。不过似乎可以通过在Component中添加标识,在子类中实现它。

应用场景

4.1 维护和展示部分-整体关系的场景,如树形菜单、文件和文件夹管理;

4.2 从一个整体中能够独立出部分模块或功能的场景;

4.3 家族关系图谱(倒推方可)

注意事项

暂无

扩展

6.1 组合模式的真实引用:依靠关系数据库的非对象存储性能,保存树形结构。

6.2 安全的组合模式:类图如下,这种做法将树枝与叶子完全分开,在遍历时需要强制类型转换(因而破坏了依赖倒置原则),也需要类型判断。可以避免运行间异常。


6.3 组合模式的遍历:给定一个结点,查找其父结点。

范例

(如上6.3,树遍历查找)


源码如下:(作者原书例)

public abstract class Corp {
	// 公司每个人都有名称
	private String name = "";
	// 公司每个人都职位
	private String position = "";
	// 公司每个人都有薪水
	private int salary = 0;
	// 父节点是谁
	private Corp parent = null;

	/*
	 * 通过接口的方式传递,我们改变一下习惯,传递进来的参数名以下划线开始 这个在一些开源项目中非常常见,一般构造函数都是定义的
	 */
	public Corp(String _name, String _position, int _salary) {
		this.name = _name;
		this.position = _position;
		this.salary = _salary;
	}

	// 获得员工信息
	public String getInfo() {
		String info = "";
		info = "姓名:" + this.name;
		info = info + "\t职位:" + this.position;
		info = info + "\t薪水:" + this.salary;
		return info;
	}

	// 设置父节点
	protected void setParent(Corp _parent) {
		this.parent = _parent;
	}

	// 等到父节点
	public Corp getParent() {
		return this.parent;
	}
}

public class Branch extends Corp {
	// 领导下边有那些下级领导和小兵
	ArrayList<Corp> subordinateList = new ArrayList<Corp>();

	// 构造函数是必须的了
	public Branch(String _name, String _position, int _salary) {
		super(_name, _position, _salary);
	}

	// 增加一个下属,可能是小头目,也可能是个小兵
	public void addSubordinate(Corp corp) {
		corp.setParent(this); // 设置父节点
		this.subordinateList.add(corp);
	}

	// 我有哪些下属
	public ArrayList<Corp> getSubordinate() {
		return this.subordinateList;
	}
}

public class Leaf extends Corp {
	// 就写一个构造函数,这个是必须的
	public Leaf(String _name, String _position, int _salary) {
		super(_name, _position, _salary);
	}
}

public class Client {
	static Leaf f;

	public static void main(String[] args) {
		// 首先是组装一个组织结构出来
		Branch ceo = compositeCorpTree();
		// 首先把CEO的信息打印出来:
		System.out.println(ceo.getInfo());
		System.out.println();
		System.out.println("开发人员 f 的上司是:" + f.getParent().getInfo());
		System.out.println();
		// 然后是所有员工信息
		System.out.println(getTreeInfo(ceo));
	}

	// 把整个树组装出来
	public static Branch compositeCorpTree() {
		// 首先产生总经理CEO
		Branch root = new Branch("王大麻子", "总经理", 100000);
		// 把三个部门经理产生出来
		Branch developDep = new Branch("刘大瘸子", "研发部门经理", 10000);
		Branch salesDep = new Branch("马二拐子", "销售部门经理", 20000);
		Branch financeDep = new Branch("赵三驼子", "财务部经理", 30000);
		// 再把三个小组长产生出来
		Branch firstDevGroup = new Branch("杨三乜斜", "开发一组组长", 5000);
		Branch secondDevGroup = new Branch("吴大棒槌", "开发二组组长", 6000);
		// 把所有的小兵都产生出来
		Leaf a = new Leaf("a", "开发人员", 2000);
		Leaf b = new Leaf("b", "开发人员", 2000);
		Leaf c = new Leaf("c", "开发人员", 2000);
		Leaf d = new Leaf("d", "开发人员", 2000);
		Leaf e = new Leaf("e", "开发人员", 2000);
		f = new Leaf("f", "开发人员", 2000);
		Leaf g = new Leaf("g", "开发人员", 2000);
		Leaf h = new Leaf("h", "销售人员", 5000);
		Leaf i = new Leaf("i", "销售人员", 4000);
		Leaf j = new Leaf("j", "财务人员", 5000);
		Leaf k = new Leaf("k", "CEO秘书", 8000);
		Leaf zhengLaoLiu = new Leaf("郑老六", "研发部副经理", 20000);
		// 开始组装
		// CEO下有三个部门经理和一个秘书
		root.addSubordinate(k);
		root.addSubordinate(developDep);
		root.addSubordinate(salesDep);
		root.addSubordinate(financeDep);
		// 研发部经理
		developDep.addSubordinate(zhengLaoLiu);
		developDep.addSubordinate(firstDevGroup);
		developDep.addSubordinate(secondDevGroup);
		// 看看开发两个开发小组下有什么
		firstDevGroup.addSubordinate(a);
		firstDevGroup.addSubordinate(b);
		firstDevGroup.addSubordinate(c);
		secondDevGroup.addSubordinate(d);
		secondDevGroup.addSubordinate(e);
		secondDevGroup.addSubordinate(f);
		// 再看销售部下的人员情况
		salesDep.addSubordinate(h);
		salesDep.addSubordinate(i);
		// 最后一个财务
		financeDep.addSubordinate(j);
		return root;
	}

	// 遍历整棵树,只要给我根节点,我就能遍历出所有的节点
	public static String getTreeInfo(Branch root) {
		ArrayList<Corp> subordinateList = root.getSubordinate();
		String info = "";
		for (Corp s : subordinateList) {
			if (s instanceof Leaf) { // 是员工就直接获得信息
				info = info + s.getInfo() + "\n";
			} else { // 是个小头目
				info = info + s.getInfo() + "\n" + getTreeInfo((Branch) s);
			}
		}
		return info;
	}
}

测试结果:

姓名:王大麻子 职位:总经理 薪水:100000

开发人员 f 的上司是:姓名:吴大棒槌 职位:开发二组组长 薪水:6000

姓名:k 职位:CEO秘书 薪水:8000

姓名:刘大瘸子 职位:研发部门经理 薪水:10000

姓名:郑老六 职位:研发部副经理 薪水:20000

姓名:杨三乜斜 职位:开发一组组长 薪水:5000

姓名:a 职位:开发人员 薪水:2000

姓名:b 职位:开发人员 薪水:2000

姓名:c 职位:开发人员 薪水:2000

姓名:吴大棒槌 职位:开发二组组长 薪水:6000

姓名:d 职位:开发人员 薪水:2000

姓名:e 职位:开发人员 薪水:2000

姓名:f 职位:开发人员 薪水:2000

姓名:马二拐子 职位:销售部门经理 薪水:20000

姓名:h 职位:销售人员 薪水:5000

姓名:i 职位:销售人员 薪水:4000

姓名:赵三驼子 职位:财务部经理 薪水:30000

姓名:j 职位:财务人员 薪水:5000


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值