Java Design Pattern Application--Combined mode

Source: Internet
Author: User

The combined pattern is actually a tree-shaped data structure. In the Windows Directory system example, how to describe a folder in the Java language?

Defines a folder class that contains a number of child file classes and several file classes.

Further abstraction, the folders and files as nodes, so a folder can be described as a node class, including a number of child nodes.

Let's look at the code for the combo pattern

//Abstract node Public Abstract classNode {protectedString name; Abstract voidAdd (node node); Abstract voidremoved (node node); Abstract voidforeach ();  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}//File class Public classFileextendsnode{@OverridevoidAdd (Node node) {} @Overridevoidremoved (node node) {} @Overridevoidforeach () {System.out.println ("File name is" +name); }         PublicFile (String name) { This. Name =name; }}//Folder classImportjava.util.ArrayList;Importjava.util.List; Public classFolderextendsnode{List<Node> nodes =NewArraylist<node>(); @OverridevoidAdd (Node node) {Nodes.Add (node); } @Overridevoidremoved (node node) {Nodes.remove (node); } @Overridevoidforeach () {System.out.println ("Folder name is" +name);  for(Node node:nodes) {Node.foreach (); }    }         PublicFolder (String name) { This. Name =name; }}

Test class

 Packagecom.coshaho.learn.component; Public classTest { Public Static voidMain (string[] args) {Folder root=NewFolder ("Root"); Folder Music=NewFolder ("Music"); Folder Software=NewFolder ("Software");        Root.add (music);        Root.add (software); File explain=NewFile ("Explain.txt"); File Moon=NewFile ("Moon.mp3"); File Word=NewFile ("Word.exe");        Root.add (explain);        Music.add (moon);        Software.add (word);    Root.foreach (); }}

In practical applications, we want to use JSON to pass the tree structure data, we want each layer of the node definition is the same, not through the class to distinguish between leaf nodes and non-leaf nodes. At this point, we can simply add a IsLeaf property to the node object to indicate whether it is a leaf node. This data structure is a recursive structure that can be parsed using recursive algorithms.

 Packagecom.coshaho.learn.component;Importjava.util.ArrayList;Importjava.util.List;Importorg.springframework.util.CollectionUtils; Public classTreeNode {PrivateString name; Private BooleanIsLeaf; Privatelist<treenode> nodes =NewArraylist<treenode>(); PrivateTreeNode (String name,Booleanisleaf) {         This. Name =name;  This. IsLeaf =IsLeaf; }        voidAdd (TreeNode node) {Nodes.Add (node); }    voidremoved (TreeNode node) {nodes.remove (node); }         Publicstring toString () {string Nameinfo= "Name:" +name; String TypeInfo= "IsLeaf:" +IsLeaf; String value=""; if(IsLeaf | |Collectionutils.isempty (nodes)) {Value= "{" + Nameinfo + "," + TypeInfo + "}"; }        Else{String Childreninfo= "Children: [";  for(TreeNode node:nodes) {childreninfo= Childreninfo + node.tostring () + ","; } childreninfo= childreninfo.substring (0, Childreninfo.length ()-1) + "]"; Value= "{" + Nameinfo + "," + TypeInfo + "," + Childreninfo + "}"; }        returnvalue; }         Public Static voidMain (string[] args) {TreeNode all=NewTreeNode ("All",false); TreeNode Guangdong=NewTreeNode ("Guangddong",true); TreeNode Sichuan=NewTreeNode ("Sichuan",false); TreeNode Chengdu=NewTreeNode ("Chengdu",true);        All.add (Guangdong);        All.add (Sichuan);        Sichuan.add (Chengdu);    System.out.println (All); }}

Java Design Pattern Application--Combined 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.