组合模式
标签:设计模式

组合模式(Composite)

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

1. 组合模式中的角色及其职责

1.1 树型结构的节点抽象(Component)

  1. 为所有的对象定义统一的接口(公共属性,行为等的定义)
  2. 提供管理子节点对象的接口方法
  3. 提供管理父节点对象的接口方法(可选)

IFile

package com.liuyao;

import java.util.List;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public interface IFile {
    public void display();

    public boolean add(IFile iFile);

    public boolean remove(IFile iFile);

    public List<IFile> getChild();
}

1.2 树型结构的叶节点(Leaf)

Component的实现子类

MyFile

package com.liuyao;

import java.util.List;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class MyFile implements IFile {
    private String name;

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

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

    @Override
    public boolean add(IFile iFile) {
        return false;
    }

    @Override
    public boolean remove(IFile iFile) {
        return false;
    }

    @Override
    public List<IFile> getChild() {
        return null;
    }
}

1.3 树型结构的枝节点(Composite)

Component的实现子类

MyFolder

package com.liuyao;

import java.util.LinkedList;
import java.util.List;

/**
 * @author liuyao
 * @date 2018/08/02
 */
public class MyFolder implements IFile{
    private String foldername;
    private List<IFile> childFile;

    public MyFolder(String foldername) {
        this.foldername = foldername;
        childFile=new LinkedList<>();
    }

    @Override
    public void display() {
        System.out.println(foldername);
    }

    @Override
    public boolean add(IFile iFile) {
        return childFile.add(iFile);
    }

    @Override
    public boolean remove(IFile iFile) {
        return childFile.remove(iFile);
    }

    @Override
    public List<IFile> getChild() {
        return childFile;
    }
}

Main

package com.liuyao;

        import java.util.List;

public class Main {

    public static void main(String[] args) {
        MyFolder rootFolder=new MyFolder("C:");
        MyFolder testFolder=new MyFolder("testFolder");
        MyFile myFile=new MyFile("test.txt");

        rootFolder.add(testFolder);
        rootFolder.add(myFile);

        MyFolder test2Folder=new MyFolder("test2Folder");
        MyFile myFile1=new MyFile("myFile1");
        testFolder.add(test2Folder);
        test2Folder.add(myFile1);

        displayTree(rootFolder,0);

    }

    private static void displayTree(IFile rootFolder,int deep) {
        for (int i = 0; i < deep; i++) {
            System.out.print("--");
        }
        rootFolder.display();
        List<IFile> childFile=rootFolder.getChild();
        for (IFile ifile: childFile) {
            if (ifile instanceof MyFile){
                for (int i = 0; i <= deep; i++) {
                    System.out.print("--");
                }
                ifile.display();
            }else {
                displayTree(ifile,deep+1);
            }
        }
    }
}

//C:
//--testFolder
//----test2Folder
//------myFile1
//--test.txt
  • 2 min read

CONTRIBUTORS


  • 2 min read