设计模式之 Composite - 组合模式

Composite模式也叫组合模式,是由GoF提出的23种软件设计模式的一种。Composite模式是构造型的设计模式之一,通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。


本文介绍设计模式中的组合(Composite)模式的概念,用法,以及实际应用中怎么样使用组合模式进行开发。
Composite模式的概念
Composite模式是构造型的设计模式之一,通过递归手段来构造诸如文件系统之类的树形的对象结构;Composite模式所代表的数据构造是一群具有统一接口界面的对象集合,并可以通过一个对象来访问所有的对象(遍历)。

Composite模式的类图描述:

[出自:wikimedia.org]

Component
树形结构的节点抽象
- 为所有的对象定义统一的接口(公共属性,行为等的定义)
- 提供管理子节点对象的接口方法
- [可选]提供管理父节点对象的接口方法

Leaf
树形结构的叶节点。Component的实现子类

Composite
树形结构的枝节点。Component的实现子类


Composite模式的应用场景
Composite模式概念起来比简单,简单一点说,可以使用Composite模式来构造一个具有统一接口界面的树形的对象群,并可通过该接口访问对象群的每个对象。


Composite模式的应用范例
我们应用Composite模式来实现文件系统的文件/目录结构:
IFile:File与Folder的共通接口界面。相当于Component。
Folder:目录。目录下面有子目录,文件。相当于Composite。
File:文件。存在于目录之中。相当于Leaf。
Client类:测试类或者说使用类。

代码:


import java.util.ArrayList;
import java.util.List;

public class Client {
    public static void main(String[] args) {
        
        //构造一个树形的文件/目录结构
        Folder rootFolder = new Folder("c:\\");
        Folder compositeFolder = new Folder("composite");
        rootFolder.addChild(compositeFolder);
        Folder windowsFolder = new Folder("windows");
        rootFolder.addChild(windowsFolder);
        File file = new File("TestComposite.java");
        compositeFolder.addChild(file);
        
        //从rootFolder访问整个对象群
        printTree(rootFolder);
    }
    
    private static void printTree(IFile ifile) {
        ifile.printName();
        List <IFile> children = ifile.getChildren();
        
        for (IFile file:children) {
            if (file instanceof File) {
                System.out.print("  ");
                file.printName();
            } else if (file instanceof Folder) {
                printTree(file);
            }
        }
    }

}


interface IFile {
    public void printName();
    
    public boolean addChild(IFile file);
    public boolean removeChild(IFile file);
    public List<IFile> getChildren();
}

class File implements IFile {
    private String name;
    
    public File(String name) {
        this.name = name;
    }
    public void printName() {
        System.out.println(name);
    }

    public boolean addChild(IFile file) {
        return false;
    }

    public boolean removeChild(IFile file) {
        return false;
    }

    public List<IFile> getChildren() {
        return null;
    }
    
}

class Folder implements IFile {
    private String name;
    private List <IFile> childList;
    
    public Folder(String name) {
        this.name = name;
        this.childList = new ArrayList<IFile>();
    }
    public void printName() {
        System.out.println(name);
    }

    public boolean addChild(IFile file) {
        return childList.add(file);
    }

    public boolean removeChild(IFile file) {
        return childList.remove(file);
    }

    public List<IFile> getChildren() {
        return childList;
    }
}


执行Client,输出结果:
C:\Composite>javac *.java
C:\Composite>java Client
c:\
composite
  TestComposite.java
windows
C:\Composite>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值