On Composite Combination Model

I. Preface

For example, if the reader has a GUI program written in java, he must define some components. After initialization, he uses the add method of container to organize these components into an interface sequentially. Or if the reader has written the front page, he must have used < div> and so on. The tag defines some formats, and then the formats are combined together to form the corresponding results in a recursive way, which is actually a combination of parts of the components embedded in the whole; or the organizational relationship between files and folders, through the directory table items as a common feature (parent class), a folder can contain multiple folders and multiple files. A file is contained in a folder. So why can we do this? There are two points to be satisfied:

  • The overall result should be a tree.
  • All components should have a common parent class

This parent class enables the common essence of components to be extracted and blended. In fact, the parent class only uses the add method, so that the subclass can be expressed by the parent class in an abstract way.

2. Code examples

Entry Abstract classes: common traits

package zyr.dp.composite;

public abstract class Entry {
    public abstract String getName();
    public abstract int getSize();
    public abstract void printList(String prefix);
    public  void printList(){
        printList("");
    }
    public  Entry add(Entry entry) throws RuntimeException{
        throw new RuntimeException();
    }
    public  String toString(){
        return getName()+"<"+getSize()+">";
    }
}

File class: implementation class, leaf node

package zyr.dp.composite;

public class File extends Entry {

    private String name;
    private int size;
    public File(String name,int size){
        this.name=name;
        this.size=size;
    }
    public String getName() {
        return name;
    }

    public int getSize() {
        return size;
    }

    public void printList(String prefix) {
        System.out.println(prefix+"/"+this);
    }

}

Directory class: Extensible nodes (intermediate nodes)

package zyr.dp.composite;

import java.util.ArrayList;
import java.util.Iterator;

public class Directory extends Entry {

    String name;
    ArrayList entrys=new ArrayList();
    public Directory(String name){
        this.name=name;
    }
    public String getName() {
        return name;
    }

    public int getSize() {
        int size=0;
        Iterator it=entrys.iterator();
        while(it.hasNext()){
            size+=((Entry)it.next()).getSize();
        }
        return size;
    }

    public Entry add(Entry entry) {
        entrys.add(entry);
        return this;
    }
    
    public void printList(String prefix) {
        System.out.println(prefix+"/"+this);
        Iterator it=entrys.iterator();
        Entry entry;
        while(it.hasNext()){
            entry=(Entry)it.next();
            entry.printList(prefix+"/"+name);
        }
    }

}

Main Class: Composite Components

package zyr.dp.composite;

public class Main {

    public static void main(String[] args) {

        Directory root=new Directory("root directory");
        
        Directory life=new Directory("My life");
        File eat=new File("Eat hot pot",100);
        File sleep=new File("Sleep",100);
        File study=new File("Study",100);
        life.add(eat);
        life.add(sleep);
        life.add(study);
        
        Directory work=new Directory("My job");
        File write=new File("Blogging",200);
        File paper=new File("Writing papers",200);
        File homework=new File("Write homework",200);
        work.add(write);
        work.add(paper);
        work.add(homework);
        
        Directory relax=new Directory("My leisure");
        File music=new File("Listen to music",200);
        File walk=new File("Go out for a walk",200);
        relax.add(music);
        relax.add(walk);
        
        Directory read=new Directory("My Reading");
        File book=new File("Learning Books",200);
        File novel=new File("Entertainment Novels",200);
        read.add(book);
        read.add(novel);
        
        root.add(life);
        root.add(work);
        root.add(relax);
        root.add(read);

        root.printList("D:");
        System.out.println("=================");
        work.printList("work");
        System.out.println("=================");
        novel.printList("novel");
        

    }

}

Operation results:

Summary

This shows that the "container + content" we used in the past is actually realized through the combination mode. The combination mode ensures the consistency of container and content. Containers can be nested or placed in the container, but the content is already a leaf node and can not continue to expand. Remember that in the abstract factory mode, in order to assemble parts into products, we made it possible to It's very interesting to use the combination mode to traverse all the content by recursion. Composite pattern is very common in our life. We must use this pattern well, understand its abstraction, especially the definition of add(), the parameter transfer between abstract class and implementation class. This is very important. Of course, we use template method and iterator. We hope you can understand the relationship between patterns and the reason why they are used each other.

Tags: Programming Java

Posted on Thu, 10 Oct 2019 02:48:06 -0700 by Jay_Seagrave