JAVA 设计模式 组合模式
用途
组合模式 (Component)
将对象组合成树形结构以表示“部分-整体”的层次结构。
组合模式使得用户对单个对象和组合对象的使用具有唯一性。
组合模式是一种结构型模式。
结构在这里插入图片描述
Component : 组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理 Component 的子部件。
package com.example.designmode.component;
/**
* <h3>design-mode</h3>
* <p>组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理 Component 的子部件。</p>
*
* @author : ZhangYuJie
* @date : 2022-02-27 15:58
**/
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}
Leaf : 表示叶节点对象。叶子节点没有子节点。
package com.example.designmode.component;
/**
* <h3>design-mode</h3>
* <p>表示叶节点对象。叶子节点没有子节点。</p>
*
* @author : ZhangYuJie
* @date : 2022-02-27 15:59
**/
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void Add(Component c) {
System.out.println("Can not add to a leaf");
}
@Override
public void Remove(Component c) {
System.out.println("Can not remove from a leaf");
}
@Override
public void Display(int depth) {
String temp = "";
for (int i = 0; i < depth; i++)
temp += '-';
System.out.println(temp + name);
}
}
Composite : 定义枝节点行为,用来存储子部件,在 Component 接口中实现与子部件相关的操作。例如 Add 和 Remove。
package com.example.designmode.component;
import java.util.ArrayList;
import java.util.List;
/**
* <h3>design-mode</h3>
* <p>定义枝节点行为,用来存储子部件,在 Component 接口中实现与子部件相关的操作。例如 Add 和 Remove。</p>
*
* @author : ZhangYuJie
* @date : 2022-02-27 16:02
**/
public class Composite extends Component {
private List<Component> children = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void Add(Component c) {
children.add(c);
}
@Override
public void Remove(Component c) {
children.remove(c);
}
@Override
public void Display(int depth) {
String temp = "";
for (int i = 0; i < depth; i++) {
temp += '-';
}
System.out.println(temp + name);
for (Component c : children) {
c.Display(depth + 2);
}
}
}
Client : 通过 Component 接口操作结构中的对象。
package com.example.designmode.component;
/**
* <h3>design-mode</h3>
* <p>通过 Component 接口操作结构中的对象。</p>
*
* @author : ZhangYuJie
* @date : 2022-02-27 16:04
**/
public class CompositePattern {
public static void main(String[] args) {
Composite root = new Composite("root");
root.add(new Leaf("Leaf A"));
root.add(new Leaf("Leaf B"));
Composite compX = new Composite("Composite X");
compX.add(new Leaf("Leaf XA"));
compX.add(new Leaf("Leaf XB"));
root.add(compX);
Composite compXY = new Composite("Composite XY");
compXY.add(new Leaf("Leaf XYA"));
compXY.add(new Leaf("Leaf XYB"));
compX.add(compXY);
root.display(1);
}
}
组合模式的优点:
● 高层模块调用简单
一棵树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。
● 节点自由增加
使用了组合模式后,我们可以看看,如果想增加一个树枝节点、树叶节点是不是都很容易,只要找到它的父节点就成,非常容易扩展,符合开闭原则,对以后的维护非常有利。
组合模式的缺点:
组合模式有一个非常明显的缺点,看到我们在场景类中的定义,提到树叶和树枝使用时的定义了吗?直接使用了实现类!这在面向接口编程上是很不恰当的,与依赖倒置原则冲突,读者在使用的时候要考虑清楚,它限制了你接口的影响范围。
使用场景:
● 维护和展示部分-整体关系的场景,如树形菜单、文件和文件夹管理。
● 从一个整体中能够独立出部分模块或功能的场景。