Composite mode (combination design mode)

Source: Internet
Author: User
Composite Design Model?

In a computer's file system, there is a "folder" concept (in some operating systems (Linux operating systems), also known as "directories "). The folder can be either a file or another folder (sub-folder ). In subfolders, both files and subfolders can be put. It can be said that folders form a container structure and recursive structure.

  • Structure Mode: This mode ensures consistency between the container and the content. The mode for creating a recursive structure is the composite mode.

  • Focus: using the composite mode can ensure consistency between the container and the content. It can also be called the multi-object and single-object consistency. Multiple objects are combined and processed as one object.

Clarify responsibilities
  • Recursive structure everywhere:
  1. In a Windows system, a window can contain a subwindow,
    2. In the document list, lists can be nested with each other, which is also a recursive structure.
    3. When multiple computer commands are combined into one macro command, if the recursive structure is used to achieve macro life.
    4. The data structure of the tree structure applies to the composite mode.
  • Implementation: demonstrate the hierarchical relationship between folder file sub-items
    Name ==================================>>> description
    Entry | abstract class, used to achieve consistency between the file class and the Directory class
    File | indicates the file class.
    Directory | indicates the folder class
    Filetreatementexception | indicates the exception class that occurs when an entry is added to the file.
    Main | class for testing program behavior

  • Location of the add () method:
  1. An exception is thrown when an entry exists.
  2. There is no entry.
  3. The Declaration is not implemented for abstract methods in the entry.
  4. Directly defined in the Directory class, but the conversion between types needs to be concerned.
UML

Class diagram:

Code
  • Entry

···

Public abstract class entry {

/*** 1. file name * 2. file Size * @ return */public abstract string getname (); public abstract int getsize (); /*** Add an entry in directory * file cannot add an entry */Public entry add (Entry entry) throws filetreatementexception {Throw new filetreatementexception ();} public void printlist () {printlist ("");} protected abstract void printlist (string prefix); @ overridepublic string tostring () {return getname () + "(" + getsize () + ") ";}

}

···

  • File
public class File extends Entry {    private String name;    private int size;    public File(String name, int size) {        this.name = name;        this.size = size;    }    @Override    public String getName() {        return name;    }    @Override    public int getSize() {        return size;    }    @Override    protected void printList(String prefix) {        System.out.println(prefix+"/"+this);    }}
  • Directory
Public class directory extends entry {private string name; private list <entry> directory = new arraylist <> (); public directory (string name) {This. name = Name ;}@ override public entry add (Entry entry) throws filetreatementexception {directory. add (entry); return this ;}@ override Public String getname () {return name ;}/ *** getsize () | printlist (string prefix) ** recursively traverse the following possible directories or sub-items of files */@ override public int getsize () {int size = 0; iterator <entry> it = directory. iterator (); While (it. hasnext () {// The entry here may be a directory or a file entry next = it. next (); size + = next. getsize () ;}return size ;}@ override protected void printlist (string prefix) {// The prefix here is a reference this will call tostring () the method will continue to call the getname () getsize () method system. out. println (prefix + "/" + this); iterator <entry> it = directory. iterator (); While (it. hasnext () {// The entry here may be a directory or a file entry next = it. next (); next. printlist (prefix + "/" + This );}}}
  • Filetreatementexception
public class FileTreatementException extends Exception {    public FileTreatementException() {    }    public FileTreatementException(String message) {        super(message);    }}
  • Target structure defined:
start +++++++++++/root(16000)/root(16000)/bin(16000)/root(16000)/bin(16000)/vi(1000)/root(16000)/bin(16000)/notepaid(15000)/root(16000)/temp(0)/root(16000)/user(0)
  • Maint
public class MainT {    public static void main(String[] args) throws FileTreatementException{        System.out.println("start +++++++++++");        Directory rootdir=new Directory("root");        Directory bindir = new Directory("bin");        Directory tempdir = new Directory("temp");        Directory userdir = new Directory("user");        rootdir.add(bindir);        rootdir.add(tempdir);        rootdir.add(userdir);        bindir.add(new File("vi",1000));        bindir.add(new File("notepaid",15000));        rootdir.printList();    }}

Composite mode (combination design mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.