• java设计模式-----13、组合模式


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

      组合(Composite)模式的其它翻译名称也很多,比如合成模式、树模式等等。在《设计模式》一书中给出的定义是:将对象以树形结构组织起来,以达成“部分-整体”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性。

     

      从定义中可以得到使用组合模式的环境为:在设计中想表示对象的“部分-整体”层次结构;希望用户忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象。

     

      

      组合模式的角色和职责

      1、Component (树形结构的节点抽象)

        1.1、为所有的对象定义统一的接口(公共属性,行为等的定义)

        1.2、提供管理子节点对象的接口方法

        1.3、[可选]提供管理父节点对象的接口方法

      2、Leaf (树形结构的叶节点)

        Component的实现子类

      3、Composite(树形结构的枝节点)

        Component的实现子类

      安全性与透明性

      组合模式中必须提供对子对象的管理方法,不然无法完成对子对象的添加删除等等操作,也就失去了灵活性和扩展性。但是管理方法是在Component中就声明还是在Composite中声明呢?

      一种方式是在Component里面声明所有的用来管理子类对象的方法,以达到Component接口的最大化(如下图所示)。目的就是为了使客户看来在接口层次上树叶和分支没有区别——透明性。但树叶是不存在子类的,因此Component声明的一些方法对于树叶来说是不适用的。这样也就带来了一些安全性问题。

     

      另一种方式就是只在Composite里面声明所有的用来管理子类对象的方法(如下图所示)。这样就避免了上一种方式的安全性问题,但是由于叶子和分支有不同的接口,所以又失去了透明性。

      《设计模式》一书认为:在这一模式中,相对于安全性,我们比较强调透明性。对于第一种方式中叶子节点内不需要的方法可以使用空处理或者异常报告的方式来解决。

       我们举个例子,比如说文件夹与文件,层次结构就符合树形结构

      首先我们新建一个Component接口(树形结构的节点抽象)

     1 /*
     2  * 文件节点抽象(是文件和目录的父类)
     3  */
     4 public interface IFile {
     5     
     6     //显示文件或者文件夹的名称
     7     public void display();
     8     
     9     //添加
    10     public boolean add(IFile file);
    11     
    12     //移除
    13     public boolean remove(IFile file);
    14     
    15     //获得子节点
    16     public List<IFile> getChild();
    17 }

       该例子符合透明性,如果是安全性,将IFile中的方法去除,不在IFile声明,直接在子类中声明即可。

      接下来,创建一个Leaf(叶子结点),因为文件是不可再分的,所以File是叶子结点

     1 /*
     2  * 文件(leaf 叶子结点)
     3  */
     4 public class File implements IFile {
     5     private String name;
     6     
     7     public File(String name) {
     8         this.name = name;
     9     }
    10     
    11 
    12     public void display() {
    13         System.out.println(name);
    14     }
    15 
    16     public List<IFile> getChild() {
    17         return null;
    18     }
    19 
    20 
    21     public boolean add(IFile file) {
    22         return false;
    23     }
    24 
    25     public boolean remove(IFile file) {
    26         return false;
    27     }
    28 
    29 }

      然后继续创建Composite(文件夹),因为文件夹下面还可能有文件与文件夹,所以是composite

     1 public class Folder implements IFile{
     2     private String name;
     3     private List<IFile> children;
     4     
     5     public Folder(String name) {
     6         this.name = name;
     7         children = new ArrayList<IFile>();
     8     }
     9     
    10     public void display() {
    11         System.out.println(name);
    12     }
    13 
    14     public List<IFile> getChild() {
    15         return children;
    16     }
    17 
    18     public boolean add(IFile file) {
    19         return children.add(file);
    20     }
    21 
    22     public boolean remove(IFile file) {
    23         return children.remove(file);
    24     }
    25 }

      然后是客户端,用递归的形式把这个具有树形结构的对象遍历出来

     1 public class MainClass {
     2     public static void main(String[] args) {
     3         //C盘
     4         Folder rootFolder = new Folder("C:");
     5         //C盘下的目录一
     6         Folder folder1 = new Folder("目录一");
     7         //C盘下的文件一
     8         File file1 = new File("文件一.txt");
     9         
    10         rootFolder.add(folder1);
    11         rootFolder.add(file1);
    12         
    13         //目录一下的目录二
    14         Folder folder2 = new Folder("目录二");
    15         //目录一下的文件二
    16         File file2 = new File("文件二.txt");
    17         folder1.add(folder2);
    18         folder1.add(file2);
    19         
    20         //目录二下的目录三
    21         Folder folder3 = new Folder("目录三");
    22         //目录二下的文件三
    23         File file3 = new File("文件三.txt");
    24         folder2.add(folder3);
    25         folder2.add(file3);
    26         
    27         displayTree(rootFolder,0);
    28         
    29     }
    30     
    31     public static void displayTree(IFile rootFolder, int deep) {
    32         for(int i = 0; i < deep; i++) {
    33             System.out.print("--");
    34         }
    35         //显示自身的名称
    36         rootFolder.display();
    37         //获得子树
    38         List<IFile> children = rootFolder.getChild();
    39         //遍历子树
    40         for(IFile file : children) {
    41             if(file instanceof File) {
    42                 for(int i = 0; i <= deep; i++) {
    43                     System.out.print("--");
    44                 }
    45                 file.display();
    46             }else {
    47                 displayTree(file,deep + 1);
    48             }
    49         }
    50     }
    51 }

      这样子,就把,这棵树遍历了出来。

      优缺点

        优点:

        1) 使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关心自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。

        2)更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。这一点符合开闭原则的要求,对系统的二次开发和功能扩展很有利!

        缺点:

        组合模式不容易限制组合中的构件。

      总结

      组合模式是一个应用非常广泛的设计模式,它本身比较简单但是很有内涵,掌握了它对你的开发设计有很大的帮助。

  • 相关阅读:
    奉上简单的.Net后端开发模板
    C#之委托如此简单
    cordova环境搭建
    Linux实现免密码登录
    RHEL7网络管理NetworkManager和nmcli指令
    RHEL7中配置本地YUM软件源
    RHEL7 网口绑定Network Teaming
    Linux工具之top
    Linux工具之ss
    linux工具之lsof
  • 原文地址:https://www.cnblogs.com/xiaobai1226/p/8567280.html
Copyright © 2020-2023  润新知