设计模式-组合模式

2019-12-16  本文已影响0人  蜗牛写java

设计模式-组合模式

概念

将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性

组合模式也叫合成模式,有时也叫部分-整体模式;主要用来描述部分与整体的关系

类型:结构型

使用场景

优点

缺点

UML

组合模式UML.png

实例

我们以《深入理解Java虚拟机》这本书的目录为例

UML


组合实例UML.png

抽象构件角色

/**
 * 书本目录
 */
public class CatalogComponent {

    //名称
    private String name;
    //页码
    private int page;

    public CatalogComponent(String name, int page) {
        this.name = name;
        this.page = page;
    }

    public void add(CatalogComponent catalogComponent) {
        throw new UnsupportedOperationException("不支持添加操作!");
    }

    public void remove(CatalogComponent catalogComponent) {
        throw new UnsupportedOperationException("不支持删除操作!");
    }

    public void list() {
        throw new UnsupportedOperationException("不支持列表操作!");
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
}

数据构件

public class ChapterCatalog extends CatalogComponent {

    private List<CatalogComponent> chapters = new ArrayList<>();

    public ChapterCatalog(String name, int page) {
        super(name, page);
    }

    @Override
    public void add(CatalogComponent catalogComponent) {
        chapters.add(catalogComponent);
    }

    @Override
    public void remove(CatalogComponent catalogComponent) {
        chapters.remove(catalogComponent);
    }

    @Override
    public String toString() {
        return this.getName() + "---------" + this.getPage();
    }

    @Override
    public void list() {
        System.out.println(this.toString());
        for (CatalogComponent chapter : chapters) {
            if (chapter instanceof ChapterCatalog) {
                chapter.list();
            } else {
                System.out.println("    " + chapter.toString());
            }
        }
    }
}

叶子构件

public class Chapter extends CatalogComponent {

    public Chapter(String name, int page) {
        super(name, page);
    }

    @Override
    public String toString() {
        return this.getName() + "---------" + this.getPage();

    }
}

使用场景

/**
 * 我们以深入理解Java虚拟机为例;只做两层目录demo
 */
public class Client {

    public static void main(String[] args) {

        CatalogComponent javaJvmBook = new ChapterCatalog("深入理解Java虚拟机",0);

        CatalogComponent firstChapterCatalog = new ChapterCatalog("走进java", 2);
        CatalogComponent firstChapterOne = new Chapter("概述", 2);
        CatalogComponent firstChapterTwo = new Chapter("java技术体系", 5);
        CatalogComponent firstChapterThree = new Chapter("java虚拟机发展史", 9);
        firstChapterCatalog.add(firstChapterOne);
        firstChapterCatalog.add(firstChapterTwo);
        firstChapterCatalog.add(firstChapterThree);

        CatalogComponent secondChapterCatalog = new ChapterCatalog("java内存区域与内存溢出异常", 38);
        CatalogComponent secondChapterOne = new Chapter("概述", 38);
        CatalogComponent secondChapterTwo = new Chapter("运行时数据区域", 38);
        CatalogComponent secondChapterThree = new Chapter("HotSpot虚拟机对象探秘", 43);
        secondChapterCatalog.add(secondChapterOne);
        secondChapterCatalog.add(secondChapterTwo);
        secondChapterCatalog.add(secondChapterThree);

        CatalogComponent thirdChapterCatalog = new ChapterCatalog("垃圾收集器与内存分配策略", 61);
        CatalogComponent thirdChapterOne = new Chapter("概述", 61);
        CatalogComponent thirdChapterTwo = new Chapter("垃圾收集算法", 62);
        CatalogComponent thirdChapterThree = new Chapter("垃圾收集器", 69);
        thirdChapterCatalog.add(thirdChapterOne);
        thirdChapterCatalog.add(thirdChapterTwo);
        thirdChapterCatalog.add(thirdChapterThree);

        javaJvmBook.add(firstChapterCatalog);
        javaJvmBook.add(secondChapterCatalog);
        javaJvmBook.add(thirdChapterCatalog);

        javaJvmBook.list();
    }
}

输出:

深入理解Java虚拟机---------0
走进java---------2
概述---------2
java技术体系---------5
java虚拟机发展史---------9
java内存区域与内存溢出异常---------38
概述---------38
运行时数据区域---------38
HotSpot虚拟机对象探秘---------43
垃圾收集器与内存分配策略---------61
概述---------61
垃圾收集算法---------62
垃圾收集器---------69

上一篇下一篇

猜你喜欢

热点阅读