首页 > 编程 > Java > 正文

Java使用组合模式实现表示公司组织结构功能示例

2019-11-26 09:58:34
字体:
来源:转载
供稿:网友

本文实例讲述了Java使用组合模式实现表示公司组织结构功能。分享给大家供大家参考,具体如下:

一、模式定义

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

二、组合模式举例

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 面向抽象编程

四、使用场合

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

组合模式的静态类图如下

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表