组合模式
定义
将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示。
使客户端对单个对象和组合对象保持一致的方式处理。
属于结构型模式。
适用场景
- 希望客户端可以忽略组合对象与单个对象的差异时;
- 对象层次具备整体和部分,呈树形结构(如树形菜单,操作系统目录结构,公司组织架构等)
组合的各个节点拥有相同的生命周期。
比如动物的头和四肢,就是组合关系。
比如电脑和CPU,就是组合关系。
比如公司的组织架构就是组合模式。
比如操作系统的资源管理器,也是组合模式。
比如班级里的学生和老师,就不是组合关系,而是聚合关系。
比如电脑与上面插入的优盘,就不是组合关系,就是聚合关系。
标准示例
组合模式有两种写法:透明写法 和 安全写法。
透明写法和安全写法的区别在于:
透明写法中,Composite和Leaf 中的方法,都是继承自Component,即便是Leaf中不需要的方法,也会继承下来默认实现。
安全写法中,Component中,只包含Composite和 Leaf 中共有的方法,Composite中独有的方法,Composite自己定义与实现。
两者写法的选择:
如果各层次有相同的行为,就可以采用透明写法。
如果各层次差异比较大,则采用安全写法。
我们针对上图左边的 transparent 写法,给出如下示例:
Component
组件,定义了组合中所有对象的通用接口。它声明了用于访问和管理子组件的方法。
public abstract class Component {
protected String name ;
public Component(String name){
this.name = name;
}
public abstract void display(int depth);
public boolean addChild(Component component){
throw new UnsupportedOperationException("addChild not supported.");
}
public boolean removeChild(Component component){
throw new UnsupportedOperationException("removeChild not supported.");
}
public Component getChild(int index){
throw new UnsupportedOperationException("getChild not supported.");
}
}
Composite
复合节点,表示组合中的复合对象,可以包含子节点(子节点既可以是其他复合节点,也可以是叶子节点)。实现了组件接口的
方法,包括管理子组件的方法。
public class Composite extends Component{
private List<Component> componentList;
public List<Component> getComponentList(){
return this.componentList;
}
public Composite(String name) {
super(name);
componentList = new ArrayList<Component>();
}
public void display(int depth) {
StringBuilder sbd = new StringBuilder();
for(int i=0;i<depth;i++){
sbd.append("-");
}
System.out.println(sbd.toString() + this.name);
for(Component component:componentList){
component.display(depth+2);
}
}
@Override
public boolean addChild(Component component) {
return this.componentList.add(component);
}
}
Leaf
叶子节点,表示组合中的叶子节点对象,叶子节点没有子节点。实现了组件接口的方法,但通常不包含子组件。
public class Leaf extends Component{
public Leaf(String name) {
super(name);
}
public void display(int depth) {
StringBuilder sbd = new StringBuilder();
for(int i=0;i<depth;i++){
sbd.append("-");
}
System.out.println(sbd.toString() + this.name);
}
}
ClientTest
客户端通过组件接口与组合结构进行交互,客户端不需要区分叶子节点和复合节点,可以一致地对待整体和部分。
public class ClientTest {
public static void main(String[] args) {
//哺乳动物
Component mammal = new Composite("哺乳动物");
//whale
Component whale = new Composite("鲸");
//鲸是哺乳动物的子类
mammal.addChild(whale);
//加入鲸的子类
whale.addChild(new Leaf("虎鲸"));
whale.addChild(new Leaf("白鲸"));
mammal.display(2);
}
}
输出结果:
--哺乳动物
----鲸
------虎鲸
------白鲸
我们常用的操作系统中,有文件目录,它包括文件夹和文件。
我们就以文件结构为例,展示一下安全写法。
Directory
,目录,抽象组件。
public abstract class Directory {
protected String name;
public Directory(String name){
this.name = name;
}
public abstract void display();
}
Folder
,文件夹,复合节点。
public class Folder extends Directory{
private List<Directory> dirs;
private Integer level;
public Folder(String name,int level) {
super(name);
this.level = level;
dirs = new ArrayList<Directory>();
}
public boolean addChild(Directory directory){
return this.dirs.add(directory);
}
public boolean removeChild(Directory directory){
return this.dirs.remove(directory);
}
public void display() {
System.out.println(this.name);
for(Directory dir:this.dirs){
if(this.level!=null){
for(int i=0;i<this.level;i++){
System.out.print("--");
}
}
//打印叶子节点名称
dir.display();
}
}
}
File
,文件,叶子节点。
public class File extends Directory{
public File(String name) {
super(name);
}
public void display() {
System.out.println(this.name);
}
}
ClientTest
public class ClientTest {
public static void main(String[] args) {
Folder root = new Folder("E盘",1);
Folder chat = new Folder("聊天软件",2);
Folder office = new Folder("办公软件",2);
File wx = new File("微信");
File qq = new File("QQ");
File word = new File("word");
File excel = new File("excel");
chat.addChild(wx);
chat.addChild(qq);
office.addChild(word);
office.addChild(excel);
root.addChild(chat);
root.addChild(office);
root.display();
}
}
输出结果为:
E盘
--聊天软件
----微信
----QQ
--办公软件
----word
----excel
以上就是组合模式全部内容,感谢阅读。