Design pattern--Combined mode

Source: Internet
Author: User

the Leaf object and the combined object implement the same interface. This is why the combination pattern can handle leaf nodes and object nodes uniformly. Basic Concepts:The combination mode [structural design pattern] Defines how the container object and the leaf object are combined recursively, so that the customer does not need to differentiate between the use of the process and can be treated in a consistent way. role of the combined mode:
    1. Component: An object declaration interface in a combination that implements the default behavior for all classes of common interfaces, where appropriate. Declares an interface for accessing and managing component subassemblies.
    2. Leaf: the Leaf object. Leaf nodes have no sub-nodes.
    3. Composite: A container object that defines the behavior of a minor point, which is used to store subassemblies, and to implement operations related to subassemblies in the component interface, such as add (add) and delete (remove).
combination Mode pros and cons:1. Making the client invocation simple, the client can consistently use a composite structure or a single object, and the user does not have to deal with a single object or the entire composition structure, which simplifies the client code.
2. It is easier to add object parts to the body. The client does not have to change the code because it joins a new object part.
 Disadvantages:To make the design more abstract, and if the business rules of an object are complex, it is challenging to implement a composite pattern, and not all methods are associated with the leaf object subclass .
 The first is to define an interface that contains an abstract method of the properties of the leaf and the container: The folder is a container,Composite, container object; file isLeaf , leaf node; IFile,Component, the object declaration interface in the composition, where appropriate, implements the default behavior for all classes of common interfaces.
  1. /** File Node abstraction (is the parent class of files and directories)*/ Public InterfaceIFile {//displays the name of the file or folder     Public voiddisplay (); //Add     Public BooleanAdd (IFile file); //removed from     Public BooleanRemove (IFile file); //Get child nodes     PublicList<ifile>getchild ();}

    IFile Implementation class: folder Class Folder.java, container object, inside can add files, delete files;

  1. Importjava.util.ArrayList;Importjava.util.List;/*** Folder class *@authorSoyoungboy **/ Public classFolderImplementsifile{PrivateString name; PrivateList<ifile>children;  PublicFolder (String name) { This. Name =name; Children=NewArraylist<ifile>(); }        /*** Show folder name*/     Public voiddisplay () {System.out.println (name); }    /*** folder may have files, so display*/     PublicList<ifile>Getchild () {returnchildren; }    /*** Add files under folder*/     Public BooleanAdd (IFile file) {returnchildren.add (file); }    /*** Delete files under folder*/     Public BooleanRemove (IFile file) {returnchildren.remove (file); }}

  2. IFile implementation class: child node, File.java
  1. Importjava.util.List;/*** File class, No child nodes, so no getchild return content, file is not conditional files, can not delete files, so Add,remove is false *@authorSoyoungboy **/ Public classFileImplementsIFile {PrivateString name;  PublicFile (String name) { This. Name =name; }    /*** File Display*/     Public voiddisplay () {System.out.println (name); }    /*** There are no files under the file*/     PublicList<ifile>Getchild () {return NULL; }    /*** Add Files*/     Public BooleanAdd (IFile file) {return false; }    /*** Delete Files*/     Public BooleanRemove (IFile file) {return false; }}


Test class:
Importjava.util.List; Public classMainClass { Public Static voidMain (string[] args) {//C-PlateFolder RootFolder =NewFolder ("C:"); //First directoryFolder Firstfolder =NewFolder ("First"); //first.txt FileFile Firstfile =NewFile ("First.txt");        Rootfolder.add (Firstfolder);                Rootfolder.add (Firstfile); //Second DirectoryFolder Secondfolder =NewFolder ("Second"); File Secondfile=NewFile ("Second.txt"); //Add level Two directoryFirstfolder.add (Secondfolder); //add files to the level two directory Second.txtFirstfolder.add (Secondfile); Folder Thirdfolder=NewFolder ("Third"); File Thirdfile=NewFile ("Third.txt"); //Add level Three directorySecondfolder.add (Thirdfolder); //Add level Three file Third.txtSecondfolder.add (Thirdfile); DisplayTree (RootFolder,0); }         Public Static voidDisplayTree (IFile RootFolder,intDeep ) {         for(inti = 0; i < deep; i++) {System.out.print ("--"); }        //show name of itselfRootfolder.display (); //Get subtreelist<ifile> children =Rootfolder.getchild (); //traversing subtrees         for(IFile file:children) {//if it's a file.            if(FileinstanceofFile) {                 for(inti = 0; I <= deep; i++) {System.out.print ("--"); }                //call the display operation directlyFile.display (); } Else {            //if it is a directory, the recursive operationDisplayTree (File,deep + 1); }        }    }}

C:--first----Second------Third------Third.txt----Second.txt--first.txtusage Scenarios in Android:generally deals with the contents of a tree structure. the structure of viewgroup and view is a combination pattern:http://www.cnblogs.com/yemeishu/archive/2013/01/06/2847156.htmlhttp://www.cnblogs.com/qianxudetianxia/archive/2011/07/29/2121488.htmlhttp://blog.csdn.net/GooHong/article/category/2314977

Design pattern--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.