设计模式——组合模式
Posted shenqiaqia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式——组合模式相关的知识,希望对你有一定的参考价值。
这次来看下组合模式,先看下frist的定义:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
那在来看下类图吧:
来看下具体代码吧:
package com.shenqi.mode; import java.util.List; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:46 */ public abstract class Department public String name; public int headcount; public List<Department> departments; public abstract void addDepartment(Department department); public abstract void removeDepartment(Department department); public String getName() return name; public void setName(String name) this.name = name; public int getHeadcount() return headcount; public void setHeadcount(int headcount) this.headcount = headcount; public List<Department> getDepartments() return departments; public void setDepartments(List<Department> departments) this.departments = departments; public void print() System.out.println("部门名称:" + name + ",人数:" + headcount);
package com.shenqi.mode; import java.util.ArrayList; import java.util.List; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:48 */ public class ApplicationPlatformDepartment extends Department public ApplicationPlatformDepartment(String name, int headcount) this.name = name; this.headcount = headcount; departments = new ArrayList<>(); @Override public void addDepartment(Department department) departments.add(department); @Override public void removeDepartment(Department department) departments.remove(department);
package com.shenqi.mode; import java.util.ArrayList; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:50 */ public class TechnologyPlatformDepartment extends Department public TechnologyPlatformDepartment(String name, int headcount) this.name = name; this.headcount = headcount; departments = new ArrayList<>(); @Override public void addDepartment(Department department) departments.add(department); @Override public void removeDepartment(Department department) departments.remove(department);
package com.shenqi.mode; import java.util.ArrayList; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:52 */ public class OrderGroup extends Department public OrderGroup(String name, int headcount) this.name = name; this.headcount = headcount; departments = new ArrayList<>(); @Override public void addDepartment(Department department) @Override public void removeDepartment(Department department)
package com.shenqi.mode; import java.util.ArrayList; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:57 */ public class ITCenter extends Department public ITCenter(String name, int headcount) this.name = name; this.headcount = headcount; departments = new ArrayList<>(); @Override public void addDepartment(Department department) departments.add(department); @Override public void removeDepartment(Department department) departments.remove(department);
package com.shenqi.mode; import java.util.List; /** * @Author:shenqi * @Description: * @Date:2019/7/13 18:55 */ public class DepartmentMain public static void main(String[] args) Department itCenter = new ITCenter("科技与信息中心",1000); Department applicationPlatformDepartment = new ApplicationPlatformDepartment("应用平台部", 55); Department technologyPlatformDepartment = new TechnologyPlatformDepartment("技术平台部", 20); Department orderGroup = new OrderGroup("订单平台组", 10); itCenter.addDepartment(applicationPlatformDepartment); itCenter.addDepartment(technologyPlatformDepartment); applicationPlatformDepartment.addDepartment(orderGroup); List<Department> departments = itCenter.getDepartments(); for(Department department : departments) department.print(); for(Department dd : department.getDepartments()) dd.print();
以上是关于设计模式——组合模式的主要内容,如果未能解决你的问题,请参考以下文章