组合模式——公司组织结构

一 模式定义
组合模式:将对象组合成树形结构以表示“部分一整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。

二 组合模式举例
1 模式分析
我们借用公司组织结构图来说明这一模式。
经过分析后,我们得出该模式静态类图如下:


 
2代码示例
2.1建立员工抽象类

package com.demo.composite;

/**
 * 职工类接口
 * 
 * @author
 * 
 */
public abstract class Staff {
	// 员工号
	protected String no;
	// 职工名字
	protected String name;
	// 职位
	protected String position;
	// 薪资
	protected float salary;

	// 私有属性 长度字符串
	private int length;

	// 构造方法
	public Staff(String no, String name, String position, float salary) {
		this.no = no;
		this.name = name;
		this.position = position;
		this.salary = salary;

		// 计算总字节长度
		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;

	}

	// 获得用户基本信息
	public void printUserBaseInfo() {
		System.out.println("|" + this.no + " " + this.name + " "
				+ this.position + " " + this.salary);
	}

	// 添加员工信息
	public abstract void add(Staff staff);

	// 删除员工
	public abstract Staff remove(String no);

	// 打印员工信息
	public abstract void printEmployeesInfo(int layer);

	// 打印若干字符
	protected void printChar(int layer) {
		for (int j = 0; j < layer * 2; j++) {
			System.out.print("-");
		}
	}

	// 私有方法打印一行
	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 创建管理者

package com.demo.composite.sub;

import java.util.ArrayList;

import com.demo.composite.Staff;

/**
 * 管理人员(手下有其他员工的人)
 * 
 * @author
 * 
 */
public class Manager extends Staff {
	// 存储手下员工信息
	private final ArrayList<Staff> arrayList = new ArrayList<Staff>();

	// 构造方法
	public Manager(String no, String name, String position, float salary) {
		super(no, name, position, salary);
	}

	/**
	 * 增加一个员工
	 */
	@Override
	public void add(Staff staff) {
		this.arrayList.add(staff);
	}

	/**
	 * 删除员工信息
	 */
	@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;
	}

	/**
	 * 打印员工信息
	 */
	@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;
			}

			// 打印'-'
			printChar(tmpLayer);
			// 打印员工基本信息
			this.arrayList.get(i).printUserBaseInfo();

			// 打印手下员工信息
			this.arrayList.get(i).printEmployeesInfo(tmpLayer);
		}
	}
}

2.3创建普通员工

package com.demo.composite.sub;

import com.demo.composite.Staff;

/**
 * 普通员工(真正干活的人)
 * 
 * @author
 * 
 */
public class Employees extends Staff
{
	// 构造方法
	public Employees(String no, String name, String position, float salary)
	{
		super(no, name, position, salary);
	}

	/**
	 * 添加员工信息
	 */
	@Override
	public void add(Staff staff)
	{
		return;
	}

	/**
	 * 删除员工信息
	 */
	@Override
	public Staff remove(String no)
	{
		// 直接返回null
		return null;
	}

	/**
	 * 打印员工信息
	 */
	@Override
	public void printEmployeesInfo(int layer)
	{
		return;
	}

}

2.4客户端测试

package com.demo;

import com.demo.composite.Staff;
import com.demo.composite.sub.Employees;
import com.demo.composite.sub.Manager;

/**
 * 主应用程序
 * 
 * @author
 * 
 */
public class Client
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// 公司CEO
		Staff boss = new Manager("1", "大老板", "CEO", 100000);
		/**
		 * CEO手下有若干部门经理
		 */
		// 财务部经理
		Staff financeManager = new Manager("11", "张总", "财务部经理", 60000);

		// 人事部经理
		Staff personnelManager = new Manager("12", "王总", "人事部经理", 60000);

		// 技术部经理
		Staff technicalManager = new Manager("13", "陈总", "技术部经理", 60000);
		/**
		 * 技术部门还有助理和若干主管
		 */
		// 技术部门助理
		Staff deptAssistant = new Manager("1301", "王助理", "部门助理", 20000);
		// 技术部门主管1
		Staff deptManager1 = new Manager("1302", "主管1", "技术主管", 30000);
		/**
		 * 技术主管deptManager1 下面还有软件工程师(最终干活的人)
		 */
		Staff softwareEngineer1 = new Employees("1302001", "张三", "软件工程师", 5000);
		Staff softwareEngineer2 = new Employees("1302002", "李四", "软件工程师", 5500);
		Staff softwareEngineer3 = new Employees("1302003", "王五", "软件工程师", 4500);
		// 为技术主管1添加员工信息
		deptManager1.add(softwareEngineer1);
		deptManager1.add(softwareEngineer2);
		deptManager1.add(softwareEngineer3);

		// 技术部门主管2
		Staff deptManager2 = new Manager("1303", "主管2", "技术主管", 30000);

		// 为技术部经理 添加:部门助理、技术主管1和技术主管2
		technicalManager.add(deptAssistant);
		technicalManager.add(deptManager1);
		technicalManager.add(deptManager2);

		// 市场部经理
		Staff marketingManager = new Manager("14", "吴总", "市场部经理", 60000);

		// 为CEO 添加:财务部经理、人事部经理、技术部经理和市场部经理
		boss.add(financeManager);
		boss.add(personnelManager);
		boss.add(technicalManager);
		boss.add(marketingManager);

		// 打印CEO 信息
		boss.printUserBaseInfo();
		// 打印CEO 手下员工信息
		boss.printEmployeesInfo(1);
	}
}

运行结果如下:

|1 大老板 CEO 100000.0
----|11 张总 财务部经理 60000.0
----|12 王总 人事部经理 60000.0
----|13 陈总 技术部经理 60000.0
------|1301 王助理 部门助理 20000.0
------|1302 主管1 技术主管 30000.0
--------|1302001 张三 软件工程师 5000.0
--------|1302002 李四 软件工程师 5500.0
--------|1302003 王五 软件工程师 4500.0
------|1303 主管2 技术主管 30000.0
----|14 吴总 市场部经理 60000.0

三 该模式设计原则

1 统一对待个别对象和组合对象
2 面向抽象编程

扫描二维码关注公众号,回复: 285766 查看本文章

四 使用场合
1 想表示对象的“部分一整体”层次结构的时候。
2 希望用户忽略组合对象与单个对象的不同,用户将统一使用组合结构中所有对象的时候。
组合模式的静态类图如下


 

猜你喜欢

转载自cakin24.iteye.com/blog/2326714
今日推荐