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:
- 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:
- An exception is thrown when an entry exists.
- There is no entry.
- The Declaration is not implemented for abstract methods in the entry.
- Directly defined in the Directory class, but the conversion between types needs to be concerned.
UML
Class diagram:
Code
···
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 () + ") ";}
}
···
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); }}
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 );}}}
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)
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)