一,组合模式
组合模式:
- 又叫部分整体模式,创建了对象组的树形结构,将对象组合成树状结构以表示“整体与部分”的层次关系;
- 依据树形结构来组合对象,用来表示部分与整体的关系;
- 使用时对单个对象和组合对象的访问具有一致性;
二,原理类图
意图: 将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。
适用性:
- 你想表示对象的部分-整体层次结构。
- 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
三,实例
component
package com.neei.component;
/**
* @param
* @Author: AaNeei
* @Date: 2019/10/9 21:19
* @Description: 游学网
* @throws:
*/
public abstract class OrganizationComponent {
private String name;
private String desc;
protected void add(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
public OrganizationComponent(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
protected abstract void print();
}
Composite
package com.neei.component;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @Author: AaNeei
* @Date: 2019/10/9 21:22
* @Description: 游学网
* @throws:
*/
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public University(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getDesc() {
return super.getDesc();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println("--------" + getName() + "---------");
for (OrganizationComponent component : organizationComponents) {
component.print();
}
}
}
Composite
package com.neei.component;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @Author: AaNeei
* @Date: 2019/10/9 21:27
* @Description: 游学网
* @throws:
*/
public class College extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public College(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getDesc() {
return super.getDesc();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println("--------" + getName() + "---------");
for (OrganizationComponent component : organizationComponents) {
component.print();
}
}
}
leaf
package com.neei.component;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @Author: AaNeei
* @Date: 2019/10/9 21:28
* @Description: 游学网
* @throws:
*/
public class Department extends OrganizationComponent {
public Department(String name, String desc) {
super(name, desc);
}
@Override
public String getDesc() {
return super.getDesc();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println("--------" + getName() + "---------");
}
}
调用
package com.neei.component;
/**
* @param
* @Author: AaNeei
* @Date: 2019/10/9 21:30
* @Description: 游学网
* @throws:
*/
public class Client {
public static void main(String[] args) {
OrganizationComponent university = new University("清华", "国际名校");
OrganizationComponent college1 = new College("计算机", "都是大神");
OrganizationComponent college2 = new College("机械", "都是大神");
college1.add(new Department("java", "牛逼"));
college1.add(new Department("C#", "牛逼"));
college2.add(new Department("机器人", "牛逼"));
college2.add(new Department("自动化", "牛逼"));
university.add(college1);
university.add(college2);
university.print();
}
}
四,源码分析
JDK源码中使用的组合模式,如HashMap;