java设计模式之——组合模式(结构型模式 )

接口的作用是什么?定义共性 组合模式:主要是 用来描述整体与部分的关系,用的最多的地方就是树形结构,我们先来说说组合模式的几个角色: 抽象构件角色(Component):定义参加组合的对象的共有方法和属性,可以定义一些默认的行为或属性; 比如我们例子中的getInfo 就封装到了抽象类中。 叶子构件(Leaf):叶子对象,其下再也没有其他的分支。 树枝构件(Composite):树枝对象,它的作用是组合树枝节点和叶子节点; 组合模式有两种模式,透明模式和安全模式,这两个模式有什么区别呢 组合模式的优点有哪些呢?第一个优点只要是树形结构,就要考虑使用组合模式,这个一定记住,只 要是要体现局部和整体的关系的时候,而且这种关系还可能比较深,考虑一下组合模式吧。组合模式有一 个非常明显的缺点:使用了实现类而不是接口,违背了面向接口编程

package com.createtype.desginpatterns.composite3;

/**
 * @author cbf4Life [email protected] I'm glad to share my knowledge with you
 *         all. 普通员工很简单,就写一个构造函数就可以了
 */
public class Leaf extends Corp {
	// 就写一个构造函数,这个是必须的
	public Leaf(String _name, String _position, int _salary) {
		super(_name, _position, _salary);
	}
}

package com.createtype.desginpatterns.composite3;

/**
 * @author cbf4Life [email protected] I'm glad to share my knowledge with you
 *         all. 定义一个公司的人员的抽象类
 */
@SuppressWarnings("all")
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;
	}
}
package com.createtype.desginpatterns.composite3;

import java.util.ArrayList;

/**
 * @author cbf4Life [email protected] I'm glad to share my knowledge with you
 *         all. 组装这个树形结构,并展示出来 您的设计模式 第 166 页
 */
@SuppressWarnings("all")
public class Client {
	public static void main(String[] args) {
		// 首先是组装一个组织结构出来
		Branch ceo = compositeCorpTree();
		// 首先把CEO的信息打印出来:
		System.out.println(ceo.getInfo());
		// 然后是所有员工信息
		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);
		Leaf 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;
	}
}

package com.createtype.desginpatterns.composite3;

import java.util.ArrayList;

/**
 * @author cbf4Life [email protected] I'm glad to share my knowledge with you
 *         all. 节点类,也简单了很多
 */
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) {
		this.subordinateList.add(corp);
		corp.setParent(this); // 设置父节点
	}

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

猜你喜欢

转载自lvwenwen.iteye.com/blog/1549415