温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Java中利用组合模式实现公司组织结构功能

发布时间:2021-05-24 18:02:47 来源:亿速云 阅读:317 作者:Leah 栏目:编程语言

怎么在Java中利用组合模式实现公司组织结构功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、模式定义

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

二、组合模式举例

1 模式分析

我们借用公司组织结构图来说明这一模式。

经过分析后,我们得出该模式静态类图如下:

怎么在Java中利用组合模式实现公司组织结构功能

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中的集合主要分为四类:1、List列表:有序的,可重复的;2、Queue队列:有序,可重复的;3、Set集合:不可重复;4、Map映射:无序,键唯一,值不唯一。

看完上述内容,你们掌握怎么在Java中利用组合模式实现公司组织结构功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI