1.定义
将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
组合模式也叫合同模式,有时也叫部分-整体模式,主要是用来描述部分与整体的关系
2.组合模式UML
组合模式分为透明组合模式和安全组合模式
2.1透明组合模式UML
透明组合模式
2.2安全组合模式UML
安全组合模式
2.3区别
从图中可以明显看出来,透明组合模式与安全组合模式的区别
主要在于抽象类与树枝类的区别,透明组合模式的一些相关操作,都在抽象类中,而安全组合模式,关于组合使用的方法放到了具体的实现类中。
2.4角色介绍
Component 抽象构件角色 定义了参加组合对象的共有方法和属性,可以定义一些默认的行为或属性
Leaf 叶子构件 叶子对象,下面没有其他的分支了,也就是遍历的最小对象
Composite 树枝构件 树枝对象,他的作用是组合树枝节点与叶子节点形成一个树形结构。
3.简单实现
3.1透明组合模式
3.1.1 抽象构件
public abstract class Component {
public ArrayList componentList=new ArrayList<>();
public void doSomething(){
}
public abstract void add(Component compoment);
public abstract void remove(Compoment compoment);
public abstract ArrayList getChildren();
}
3.1.2 叶子构件
public class Leaf extends Component {
@Deprecated
@Override
public void add(Component compoment) throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void remove(Compoment compoment) throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public ArrayList getChildren() throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
}
添加Deprecated注解,可在对象调用方法的时候,发现是过期方法。
3.1.3 树枝构件
public class Composite extends Component {
@Override
public void add(Component compoment) {
this.componentList.add(compoment);
}
@Override
public void remove(Compoment compoment) {
this.componentList.remove(compoment);
}
@Override
public ArrayList getChildren() {
return this.componentList;
}
}
3.1.4 客户端调用
public class MyClass {
public static void main(String args[]) {
//空数据。。。假装有数据。。。
Component root=new Composite();
traverse(root);
}
//通过递归进行遍历(结果就不打印了,,毕竟是空数据。。)
private static void traverse(Component root) {
for(Component component:root.getChildren()){
if (component instanceof Leaf){
component.doSomething();
}else {
traverse(component);
}
}
}
}
3.2 安全组合模式
相比于透明组合模式,两者的区别,就是一个将相关的操作放在了抽象类中,另一个将相关的操作放在了具体的类中。
3.2.1 抽象构件
public abstract class Component {
public void doSomething(){
}
}
3.2.2 叶子构件
public class Leaf extends Component {
@Override
public void doSomething() {
//自己想加啥加啥。。。
}
}
3.2.3 树枝构件
public class Composite extends Component {
private ArrayList componentList=new ArrayList<>();
public void add(Component compoment) {
this.componentList.add(compoment);
}
public void remove(Compoment compoment) {
this.componentList.remove(compoment);
}
public ArrayList getChildren() {
return this.componentList;
}
}
3.2.4 场景调用
public class MyClass {
public static void main(String args[]) {
Component root=new Composite();
traverse((Composite) root);
}
private static void traverse(Composite root) {
for(Component component:root.getChildren()){
if (component instanceof Leaf){
component.doSomething();
}else {
traverse((Composite) component);
}
}
}
}
相比于透明组合模式,安全组合模式在进行递归的时候需要进行强转操作,毕竟相关的方法是在具体子类进行实现的而不是在抽象类中。同样因为是空数据。。。就不打印了。
4.总结
4.1优点
高层模块调用简单,一颗树形结构中所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。
节点自由增加,非常容易扩展
4.2使用场景
维护和展示部分-整体关系的场景,比如树形菜单,文件和文件夹管理
从一个整体结构中可以独立出部分模块或功能的场景。
总结而言,就是只要是树形结构的,你就可以考虑使用组合模式了。