Combination mode (partial overall mode)-structural type

Creation type

1. Singleton design pattern
2. Factory design pattern
3. Builder design pattern
4. Prototype design pattern

Structure type

5. Agency design pattern
6, bridging design pattern
7, decoration design pattern
8, adapter design pattern
9, appearance design pattern
10, flyweight design pattern
11, combination design pattern

Behavioral

12. Template design mode

Introduction

Combination mode refers to: organizing a group of objects into a tree structure to represent a "part-whole" hierarchical structure. Make users consistent in the use of single objects and combined objects;

scenes to be used

1. When representing the part-whole hierarchical structure of the object;

2. A scene where some modules or functions can be independently derived from a whole;

UML graphics

Component: abstract root node

Leaf: specific leaf nodes

Composite: specific branch nodes

Here is an example to illustrate the combination design pattern;

The file system of a computer should be familiar to everyone who has used a computer. There can be folders or files in the file; then there can be folders or files in the folder; this file management system matches the combination mode we are going to talk about today. ; The following file system is an example to illustrate the realization of the combined mode;

Code

1. Define the abstract class of a folder or file;

public abstract class FileSystemNode {
    protected String path;

    public FileSystemNode(String path) {
        this.path = path;
    }

    public abstract int countNumOfFiles();

    public abstract long countSizeOfFiles();

    public String getPath() {
        return path;
    }
}

File class

public class File extends FileSystemNode {
    public File(String path) {
        super(path);
    }

    @Override
    public int countNumOfFiles() {
        return 1;
    }

    @Override
    public long countSizeOfFiles() {
        java.io.File file = new java.io.File(path);
        if (!file.exists()) return 0;
        return file.length();
    }
}

Folder class

public class Directory extends FileSystemNode {
    private List<FileSystemNode> subNodes = new ArrayList<>();

    public Directory(String path) {
        super(path);
    }

    @Override
    public int countNumOfFiles() {
        int numOfFiles = 0;
        for (FileSystemNode fileOrDir : subNodes) {
            numOfFiles += fileOrDir.countNumOfFiles();
        }
        return numOfFiles;
    }

    @Override
    public long countSizeOfFiles() {
        long sizeofFiles = 0;
        for (FileSystemNode fileOrDir : subNodes) {
            sizeofFiles += fileOrDir.countSizeOfFiles();
        }
        return sizeofFiles;
    }

    public void addSubNode(FileSystemNode fileOrDir) {
        subNodes.add(fileOrDir);
    }

    public void removeSubNode(FileSystemNode fileOrDir) {
        int size = subNodes.size();
        int i = 0;
        for (; i < size; ++i) {
            if (subNodes.get(i).getPath().equalsIgnoreCase(fileOrDir.getPath())) {
                break;
            }
        }
        if (i < size) {
            subNodes.remove(i);
        }
    }
}

Client: Most users of the client

public class client {
    public static void main(String[] args) {
        Directory fileSystemTree = new Directory("/");
        Directory node_android = new Directory("/android/");
        Directory node_java = new Directory("/java/");
        fileSystemTree.addSubNode(node_android);
        fileSystemTree.addSubNode(node_java);
        File node_android_a = new File("/android/a.txt");
        File node_java_b = new File("/android/b.txt");
        Directory node_android_movies = new Directory("/android/movies/");
        node_android.addSubNode(node_android_a);
        node_java.addSubNode(node_java_b);
        node_android.addSubNode(node_android_movies);
        File node_android_movies_c = new File("/android/movies/c.avi");
        node_android_movies.addSubNode(node_android_movies_c);
        System.out.println("/files num:" + fileSystemTree.countNumOfFiles());
        System.out.println("/android/ files num:" + node_android.countNumOfFiles());
        System.out.println("/java/ files num:" + node_java.countNumOfFiles());
    }
}

Run log output:

/files num:3
/android/ files num:2
/java/ files num:1

 

Guess you like

Origin blog.csdn.net/ezconn/article/details/107437094