组合设计模式
一、组合设计模式
1、介绍
- 组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构。将对象组合成树状结构以表示“整体-部分”的层次关系
- 组合模式依据树形结构来组合对象,用来表示部分以及整体层次
- 组合模式属于结构型模式
- 组合模式使得用户对单个对象和组合对象的访问具有一致性。即:组合能让客户以一致的方式处理个别对象以及组合对象
2、原理UML类图
- Component:这是组合中对象声明接口,定义或实现公共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或接口
- Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作,比如增加、删除、操作
- Leaf:在组合中表示叶子节点,叶子节点没有子节点
3、组合模式优点和使用
- 简化客户端操作。客户端只需面对一致的对象而不用考虑整体部分或者节点的问题
- 具有较强扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系。客户端不用做出任何改动
- 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子,从而创建出复杂的树形结构
- 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
- 要求较高的抽象性,如果节点和叶子有很多差异性的话。比如很多方法和树形不一样,那么不适合使用组合模式
二、普通设计编程
1、介绍
公司、部门、组构成三个层次。
公司包含部门,部门包含组。
如果没有学过组合设计模式,我们一开始的解决方法会如下方所示。Company类聚合Department类,Department类聚合Group类。
此种方法的优势在于简单、容易理解。
但是对于客户端来说,操作复杂,无论我们是增加还是删除一个公司下的部门(或部门下的组),涉及的代码较多。同时这种设计也不利于管理。
2、UML
3、代码
import java.util.List;
/**
* @description: 公司
* @author: dashu
* @create: 10:43
*/
public class Company {
/**
* 名称
*/
private String name;
/**
* 部门
*/
private List<Department> departments;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Department> getDepartments() {
return departments;
}
public void setDepartments(List<Department> departments) {
this.departments = departments;
}
public void print(){
System.out.println("-----"+name+"-----");
for (Department department : departments){
System.out.println("---"+department.getName()+"---");
for (Group group : department.getGroups()){
System.out.println(group.getName());
}
}
}
}
import java.util.List;
/**
* @description: 部门
* @author: dashu
* @create: 10:55
*/
public class Department {
/**
* 名称
*/
private String name;
/**
* 组
*/
private List<Group> groups;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Group> getGroups() {
return groups;
}
public void setGroups(List<Group> groups) {
this.groups = groups;
}
public void print() {
System.out.println("---" + name + "---");
for (Group group : groups) {
System.out.println(group.getName());
}
}
}
/**
* @description: 组
* @author: dashu
* @create: 10:58
*/
public class Group {
/**
* 名称
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void print() {
System.out.println(name);
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @description: 客户端
* @author: dashu
* @create: 10:40
*/
public class Client {
public static void main(String[] args) {
Company company = new Company();
company.setName("软件开发公司");
List<Department> departments = new ArrayList<Department>();
company.setDepartments(departments);
Department department1 = new Department();
department1.setName("研发部");
List<Group> groups = new ArrayList<Group>();
department1.setGroups(groups);
Group groupA = new Group();
groupA.setName("A组");
groups.add(groupA);
Group groupB = new Group();
groupB.setName("B组");
groups.add(groupB);
departments.add(department1);
Department department2 = new Department();
department2.setName("财务部");
List<Group> groups2 = new ArrayList<Group>();
department2.setGroups(groups2);
Group groupC = new Group();
groupC.setName("C组");
groups2.add(groupC);
Group groupD = new Group();
groupD.setName("D组");
groups2.add(groupD);
departments.add(department2);
company.print();
}
}
二、组合设计模式编程
1、介绍
组合模式使得用户对单个对象和组合对象的访问具有一致性。即:组合能让客户以一致的方式处理个别对象以及组合对象
2、UML
3、代码
/**
* @description: 组织组件
* @author: dashu
* @create: 11:28
*/
public abstract class OrganizationComponent {
private String name;
public OrganizationComponent(String name){
this.name = name;
}
/**
* 添加组织
* @param organizationComponent
*/
protected void add(OrganizationComponent organizationComponent){
throw new UnsupportedOperationException();
}
/**
* 删除组织
* @param organizationComponent
*/
protected void remove(OrganizationComponent organizationComponent){
throw new UnsupportedOperationException();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 抽象打印方法
*/
protected abstract void print();
}
/**
* @description: 公司
* @author: dashu
* @create: 10:43
*/
public class Company extends OrganizationComponent{
/**
* 名称
*/
private String name;
public Company(String name) {
super(name);
}
private List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print(){
System.out.println("-----"+getName()+"-----");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @description: 部门
* @author: dashu
* @create: 10:55
*/
public class Department extends OrganizationComponent{
/**
* 名称
*/
private String name;
public Department(String name) {
super(name);
}
private List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print() {
System.out.println("---"+getName()+"---");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
/**
* @description: 组
* @author: dashu
* @create: 10:58
*/
public class Group extends OrganizationComponent{
/**
* 名称
*/
private String name;
public Group(String name) {
super(name);
}
@Override
public void print() {
System.out.println(getName());
}
}
/**
* @description: 客户端
* @author: dashu
* @create: 11:41
*/
public class Client {
public static void main(String[] args) {
OrganizationComponent component = new Company("软件开发公司");
OrganizationComponent department1 = new Department("研发部");
OrganizationComponent department2 = new Department("财务部");
OrganizationComponent a = new Group("A组");
OrganizationComponent b = new Group("B组");
OrganizationComponent c = new Group("C组");
OrganizationComponent d = new Group("D组");
department1.add(a);
department1.add(b);
department2.add(c);
department2.add(d);
component.add(department1);
component.add(department2);
component.print();
System.out.println("------------------------");
department1.print();
System.out.println("------------------------");
department2.print();
}
}
OrganizationComponent d = new Group("D组");
department1.add(a);
department1.add(b);
department2.add(c);
department2.add(d);
component.add(department1);
component.add(department2);
component.print();
System.out.println("------------------------");
department1.print();
System.out.println("------------------------");
department2.print();
}
}