一、概述
1、定义
Compose objects into tree structures to represent part-whole hierarches.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构使得用户对单个对象和组合对象的使用具有一致性)。
2、通用类图
二、通用源码
Component抽象构件角色:
定义参加组合对象的共有方法和属性,可以定义一些默认的行为和属性。
public abstract class Component {
//个题和整体都具有的共享
public void doSomething(){
//业务逻辑处理
}
}
Composite树枝构件:
树枝对象,作用是组合树枝节点和叶子节点形成一个树形结构。
public class Composite extends Component {
//构件容器
private ArrayList<Component> componentArrayList=new ArrayList<>();
//增加一个叶子构件或者树枝构件
public void add(Component component){
this.componentArrayList.add(component);
}
//删除一个叶子构件或者树枝构件
public void remove(Component component){
this.componentArrayList.remove(component)