Java设计模式学习08——组合模式

本文介绍了组合模式的基本概念,包括其适用场景、结构组成及工作流程。通过一个杀毒软件的示例,展示了如何利用组合模式来处理文件和文件夹的递归杀毒过程。

转:http://blog.youkuaiyun.com/xu__cg/article/details/53011258


一、组合模式适用场景


把部分和整体的关系用树形结构来表示,从而使客户端可以使用统一的方式对部分对象和整体对象进行管理。

二、组合模式结构


  • 抽象构件(Conponent)角色:所有类的共有接口,定义了叶子和容器的共同点。
  • 叶子(Leaf)构件角色:在组合中表示叶子结点对象,叶子节点无子节点。
  • 容器(Composite)构件角色:有容器特征,可以 用来存储子节点,在Component接口中实现与子节点有关操作,如增加(add)和删除 (remove)等。

这里写图片描述

组合模式工作流程分析


  • 组合模式为处理树形结构提供了完美的解决方案,描述了如何将容器和叶子进行递归组合,使得用户在使用时可以一致性的对待容器和叶子。
  • 当容器对象的指定方法被调用时,将遍历整个树形结构,寻找包含这个方法的成员,并调用执行。其中,使用递归调用机制对整个结构进行处理。

下面通过简单的杀毒例子,使用组合模式: 
示例代码: 
1.抽象构件

public interface AbstractFile {
    void killVirus();//杀毒
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

2. 叶子构件

class ImageFile implements AbstractFile{
    private String name;

    public ImageFile(String name){
        this.name=name;
    }

    public void killVirus() {
        System.out.println("---对图像文件"+name+"杀毒");

    }

}
class TextFile implements AbstractFile{
    private String name;

    public TextFile(String name){
        this.name=name;
    }

    public void killVirus() {
        System.out.println("---对文本文件"+name+"杀毒");

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3. 容器构件

class Folder implements AbstractFile{
    private String name;
    private ArrayList<AbstractFile> list=new ArrayList<AbstractFile>();
    public Folder(String name){
        this.name=name;
    }

    public void add(AbstractFile file){
        list.add(file);
    }
    public void remove(AbstractFile file){
        list.remove(file);
    }
    public AbstractFile getChild(int index){
        return list.get(index);
    }

    public void killVirus() {
        System.out.println("---对文件夹"+name+"杀毒");
        for(AbstractFile file:list){
            file.killVirus();
        }

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

4.客户端

public class Client {

    public static void main(String[] args) {
        Folder f1;
        AbstractFile f2,f3;

        f1=new Folder("我的收藏");
        f2=new ImageFile("my.jpg");
        f3=new TextFile("my.txt");
        f1.add(f2);
        f1.add(f3);
        f1.killVirus();

    }

}

//输出结果:
---对文件夹我的收藏杀毒
---对图像文件my.jpg杀毒
---对文本文件my.txt杀毒  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值