组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
意图:将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。
何时使用: 1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。
关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。
优点: 1、高层模块调用简单。 2、节点自由增加。
缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
组合模式的应用场景:
1.在需要表示一个对象整体与部分的层次结构的场合。
2.要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。
组合模式包含以下主要角色:
1.抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。
2.树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。
3.树枝构件(Composite)角色:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
组合模式分为透明式的组合模式和安全式的组合模式。
透明方式:在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。其结构图如图所示。
![](https://i-blog.csdnimg.cn/blog_migrate/c7e771df26652da3241e181f2dd472cb.gif)
安全方式:在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。其结构图如图所示。
![](https://i-blog.csdnimg.cn/blog_migrate/52a42c10eedfdf1a87a2d32e924145ba.gif)
实现
集合 c0={leaf1,{leaf2,leaf3}}
/**
*
* @ClassName: Component
* @Description: 透明式抽象构件
* @author: ljx
* @date: 2019年2月18日 上午11:11:23
*/
public interface Component {
public void add(Component c);
public void remove(Component c);
public Component getChild(int i);
public void operation();
}
/**
*
* @ClassName: Leaf
* @Description: 透明式叶子构件
* @author: ljx
* @date: 2019年2月18日 上午11:23:40
*/
public class Leaf implements Component {
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void add(Component c) {}
@Override
public void remove(Component c) {}
@Override
public Component getChild(int i) {
return null;
}
@Override
public void operation() {
System.out.println("透明式--叶子:"+name+"被访问");
}
}
/**
*
* @ClassName: Composite
* @Description: 透明式树枝构件
* @author: ljx
* @date: 2019年2月18日 上午11:24:22
*/
public class Composite implements Component {
private ArrayList<Component> childList = new ArrayList<Component>();
@Override
public void add(Component c) {
childList.add(c);
}
@Override
public void remove(Component c) {
childList.remove(c);
}
@Override
public Component getChild(int i) {
return childList.get(i);
}
@Override
public void operation() {
for (Component component : childList) {
component.operation();
}
}
}
/**
*
* @ClassName: TestComposite
* @Description: 测试
* @author: ljx
* @date: 2019年2月18日 上午11:28:28
*/
public class TestComposite {
public static void main(String[] args) {
Component c0 = new Composite();
Component c1 = new Composite();
Component leaf1 = new Leaf("leaf1");
Component leaf2 = new Leaf("leaf2");
Component leaf3 = new Leaf("leaf3");
c0.add(leaf1);
c0.add(c1);
c1.add(leaf2);
c1.add(leaf3);
c0.operation();
}
}
/**
*
* @ClassName: Component2
* @Description: 安全式抽象构件
* @author: ljx
* @date: 2019年2月18日 上午11:33:40
*/
public interface Component2 {
public void operation();
}
/**
*
* @ClassName: Leaf2
* @Description: 安全式叶子构件
* @author: ljx
* @date: 2019年2月18日 上午11:40:55
*/
public class Leaf2 implements Component2 {
private String name;
public Leaf2(String name) {
this.name = name;
}
@Override
public void operation() {
System.out.println("安全式--叶子:"+name+"被访问");
}
}
/**
*
* @ClassName: Composite2
* @Description: 安全式树枝构件
* @author: ljx
* @date: 2019年2月18日 上午11:40:35
*/
public class Composite2 implements Component2 {
private ArrayList<Component2> childList = new ArrayList<Component2>();
public void add(Component2 c){
childList.add(c);
}
public void remove(Component2 c){
childList.remove(c);
}
public Component2 getChild(int i){
return childList.get(i);
}
@Override
public void operation() {
for (Component2 component2 : childList) {
component2.operation();
}
}
}
public class TestComposite2 {
public static void main(String[] args) {
Composite2 c0 = new Composite2();
Composite2 c1 = new Composite2();
Leaf2 leaf1 = new Leaf2("leaf1");
Leaf2 leaf2 = new Leaf2("leaf2");
Leaf2 leaf3 = new Leaf2("leaf3");
c0.add(leaf1);
c0.add(c1);
c1.add(leaf2);
c1.add(leaf3);
c0.operation();
}
}
......,欢迎指正!