Tagged with: parent class Stringbu Prefix builder turn file class size folder
Combination Mode-composite
The composite mode makes the user consistent with the use of individual objects and composite objects.
In << schematic design pattern >> File Example: A folder is a combination of files, a file is a single object, a folder is a combination of multiple files. However, for users, whether it is a file or a folder, he wants to use a unified approach to manage them. This requires that they be abstracted again.
Reading this kind of diagram is clear:
1. The file is a component
2. The folder is also a component
3. There are many components in the folder:
3.1 files can be found in the folder
3.2 Folders can have folders
Component Abstract class
This is the abstract definition class for the file folder--the Component class
/** * Here is the uniform abstract class for files/Folders "Component Class" */public abstract class Component {/** * parent is a pointer to the parent component, which is the "parent directory" in this example */prot ected Component Parent; /** * Get the component name */public abstract String getName (); /** * Gets the size of the component */public abstract int getsize (); /** * @implNote Add a component to a component. * The leaf (leaf) node in the @implSpec combination mode can not use this method, as illustrated by example: * A file can not add a file or folder, can only be a folder to add files or folders * * Public Component Add (Co Mponent entry) {throw new RuntimeException ("Do not support this type of operation ..."); } public void Printlist () {printlist (""); } protected abstract void Printlist (String prefix); /** * Get Absolute path */public String getfullname () {StringBuilder fullName = new StringBuilder (); Component entry = this; Do {fullname.insert (0, "/" + entry.getname ()); Entry = Entry.parent; } while (entry! = NULL); return fullname.tostring (); }/** * GetSize () will be overridden by the quilt class because the file size can be easily known, but the folder size needs to be calculated recursively */ @Override public String toString () {return getName () + "(" + getsize () + ")"; }}
MyFile class
The MyFile class is a leaf (leaf) node in composite mode because he does not contain other components.
We can think of the component pattern as a tree (green indicates a folder, orange indicates a file):
The leaf node of a combined pattern refers to a file node because the file never has child nodes.
While the green C1 node is now a leaf node, if you add a file to the C1 folder, he is no longer a leaf node.
It is called leaf (leaf) that cannot continue adding child nodes in the combined mode.
The tree above is equivalent to:
public class MyFile extends Component { private String name; private int size; Public MyFile (String name, int size) { this.name = name; this.size = size; } @Override public String getName () { return this.name; } @Override public int GetSize () { return this.size; } @Override protected void printlist (String prefix) { System.out.println (prefix + "/" + this.tostring ());} }
Mydirectory class
Mydirectory class as a folder that can contain other components (Files/folders)
public class Mydirectory extends Component {private String name; Private arraylist<component> substance = new arraylist<> (); Public mydirectory (String name) {this.name = name; } @Override Public String getName () {return this.name; }/** * Gets the size of the folder: * 1. The size starts with 0 * 2. If it is a file, add the size of the file directly to * 3. If it is a folder, then recursively find out the size of the folder and add it to size. */@Override public int getsize () {int size = 0; for (Component entry:substance) {size + = Entry.getsize (); } return size; /** * Add a file or folder like a folder */@Override public Component Add (Component entry) {Substance.add (entry ); Entry.parent = this; return this; }/** * 1. Prints the path to the current directory * 2. Then print out the subdirectory, the path to the sub-file * 3. Subdirectories re-print the path of the subdirectory/double file ... So recursive */@Override protected void printlist (String prefix) {System.out.println (prefix + "/" + This.tostri Ng ()); For (Component entry: Substance) {entry.printlist (prefix + "/" + name); } }}
Main
For Test runs
public class Main {public static void main (string[] args) {/* This example directory structure is as follows: Level two directory level three directory A1 |-------B1 |-------B2 | A2 |-------C1 |-------Composite.java |-------King.python |---- ---c2 */mydirectory a1 = new Mydirectory ("A1"); Mydirectory A2 = new Mydirectory ("A2"); Mydirectory B1 = new Mydirectory ("B1"); Mydirectory b2 = new Mydirectory ("B2"); A1.add (B1). Add (B2); Mydirectory C1 = new Mydirectory ("C1"); Mydirectory C2 = new Mydirectory ("C2"); A2.add (C1). Add (C2); MyFile java = new MyFile ("Composite.java", 100); MyFile python = new MyFile ("King.python", 214); C1.add (Java). Add (Python); /*-****** getfullname () ************-*/System.out.println (Java.getfullname ()); System.out.println (Python.getfullname ()); System.out.println (""); /*-**Getfullname () ************-*/a1.printlist (); SYSTEM.OUT.PRINTLN ("-----"); A2.printlist (); }}
Combined Mode-composite (Java implementation)