设计模式-组合模式

组合模式

组合模式,也称为整体部分模式,他的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性。(树形结构)
组合与聚合的关系:组合生命周期保持一致。聚合具有不同的生命周期。

角色:
  • 抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预先定义一些默认行为和属性。
  • 树枝节点(Composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构。
  • 叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历最小的单位
应用场景:
  • 希望客户端可以忽略组合对象与单个对象的差异时;
  • 对象层次具备整体和部分,呈树形结构

组合模式,在代码具体实现上,有两种不同的方式:透明组合模式和安全组合模式
透明组合模式,直白一点地说,就是将所有需要的业务都写在抽象根节点上,并给出默认行为。树枝节点,叶子节点重写所需要方法,不需要的不重写。安全组合模式,抽象接口仅仅定义共同的逻辑,树枝节点和叶子节点各自定义自己特有的逻辑。

安全模式举例如下:

package com.jdwa.combination;

public abstract class Directory {
    protected String name;

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

    public abstract void show();
}

package com.jdwa.combination;

public class File extends Directory{

    public File(String name){
        super(name);
    }

    @Override
    public void show() {
        System.out.println(this.name);
    }
}

package com.jdwa.combination;

import com.jdwa.builder.Director;

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

public class Folder extends Directory{

    private List<Directory> dirs;

    private Integer level;

    public Folder(String name,Integer level){
        super(name);
        this.level = level;
        this.dirs = new ArrayList<>();
    }

    public void show() {
        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("    ");
                }
                for (int i = 0 ; i < this.level; i++){
                    if (i == 0 ) System.out.print("+");
                    System.out.print("-");
                }
            }
            dir.show();
        }
    }

    public boolean add(Directory dir){
        return this.dirs.add(dir);
    }

    public boolean remove(Directory dir){
        return this.dirs.remove(dir);
    }

    public Directory get(int index){
        return this.dirs.get(index);
    }

    public void list(){
        for (Directory dir : this.dirs){
            System.out.println(dir.name);
        }
    }
}
package com.jdwa.combination;

public class Client {
    public static void main(String[] args) {
        System.out.println("===========安全组合模式===========");
        File qq = new File("QQ.exe");
        File wechat = new File("Wechat.exe");

        Folder office = new Folder("办公软件",2);

        File word = new File("Word.exe");
        File ppt = new File("PPT.exe");
        File excel = new File("Excel.exe");

        office.add(word);
        office.add(ppt);
        office.add(excel);

        Folder wps = new Folder("金山软件",3);
        wps.add(new File("WPS.exe"));
        office.add(wps);

        Folder root = new Folder("根目录",1);
        root.add(qq);
        root.add(wechat);
        root.add(office);

        System.out.println("===========show()===========");
        root.show();

        System.out.println("===========list()===========");
        root.list();

    }
}

运行结果:

===========安全组合模式===========
===========show()===========
根目录
    +-QQ.exe
    +-Wechat.exe
    +-办公软件
        +--Word.exe
        +--PPT.exe
        +--Excel.exe
        +--金山软件
            +---WPS.exe
===========list()===========
QQ.exe
Wechat.exe
办公软件
欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

posted @ 2024-06-15 18:07  东方欲晓_莫道君行早  阅读(3)  评论(0编辑  收藏  举报