组合模式(Composite Pattern)

组合模式(Composite Pattern)

其目的将单个对象和组合对象用相同的接口进行表示,使得客户端对单个对象和组合对象保持一致的方式处理。简而言之就是来继承同一个抽象类,常见的例子就是系统中的文件夹,一个目录下的文件、文件夹、文件都有相同的功能(展示、新增(文件没有)、删除等),所以我们就把他抽象成一个抽象类,使得对每个对单个对象的调用都是统一的。常见的两种模式

  • 安全模式(比如文件没有新增功能,那就不能对客户端暴露这个接口)
  • 透明模式(对文件、文件夹、目录的操作都是一样的,即使没有某个个体没有某个功能我们也可以进行调用因为他们继承的是同一个接口,这就违法了最少知道原则,所以本文中不进行阐述)

一般公司的组织架构、文件夹这些都可以用这个来实现。

通常来说:组合模式需要一个抽象组件、树枝节点、和叶子节点,下面用一个文件夹的例子进行举例说。

To display

 First of all we should create root that is directory

abstract class Directory {
    Directory(String name) {
        this.name = name;
    }

    String name;
    abstract  void  show();
}

Second we should create branch that is Folder(because each files are contained in Flolders)

public class Folder extends Directory {
// save all file ,you should not put File as restrictive condition,because you do not know how many file there are List<Directory> list; // we should consider its level then according this to arrange series Integer level; public Folder(String name, Integer level) { super(name); this.level = level; this.list = new ArrayList<Directory>(); } @Override void show() { System.out.println(this.name); for (Directory dir : this.list) { // code in block of if are used to show construction of Files,you can ignore the logic 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 directory) { return this.list.add(directory); } public void list() { for (Directory directory : list) { System.out.println(directory.name); } } }

To test

public static void main(String[] args) {
        Folder root = new Folder("root",1);

        File qq = new File("QQ.exe");
        File wx = new File("WeChat.exe");


        root.add(qq);
        root.add(wx);

        // working Folder
        Folder office = new Folder("softWare of working",2);
        File word = new File("Word.exe");
        File ppt = new File("PowerPoint.exe");
        File excel = new File("Excel.exe");
        office.add(word);
        office.add(ppt);
        office.add(excel);

        //This is a son Folder that  belong to  working Folder
        Folder wps = new Folder("KingSoft",3);
        wps.add(new File("WPS.exe"));
        office.add(wps);


        root.add(office);

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

        System.out.println("----------list()-----------");
        root.list();
    }

as a  result

We can assemble them easily with Composite Pattern.

How is composite pattern applied into sorce of code

java.util.HashMap#putAll

java.util.ArrayList#addAll(java.util.Collection<? extends E>)

 apparently,Arraylist must be bound to  implements Connection(like list root we have mentioned

org.apache.ibatis.scripting.xmltags.SqlNode#apply

we open all of them at will, we will find them all pass on the same classes that is sqlNode

 

Sum up

advantage:

  • it is convenient to control the whole construction with a List( List<Directory> list)

disadvantage:

  • it is hard to design the top class of interface(we have to  extract same point then construct our top interface/class)

posted @ 2021-05-06 14:01  UpGx  阅读(65)  评论(0编辑  收藏  举报