Java_ design Pattern _ Combined Mode _composite pattern (2016-08-12)

Source: Internet
Author: User

Concept:

Combined mode (Composite pattern ) combines objects into a tree structure to represent a "partial-whole" hierarchy , combining patterns that make the user consistent with the use of individual objects and composite objects.

 sometimes called part-overall mode, it blurs the concept of simple elements and complex elements in the problem of our tree structure, and the client program can deal with complex elements like simple elements, thus making the client program and complex meta - the internal structure of the voxel is decoupled.

Class Diagram:

Involved roles:

1.Component is the object declaration interface in a composition, and, where appropriate, implements the default behavior for all classes of common interfaces. Declares an interface for accessing and managing component subassemblies.

2.Leaf represents the leaf node object in the combination, and the leaf node has no sub-nodes.

3.Composite defines a side point behavior that is used to store sub-parts and implement operations related to subassemblies in the component interface, such as add and delete (remove) and so on.

Examples are as follows:

//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 child parts of the Component. Abstract classComponent {protectedString name;  PublicComponent (String name) { This. Name =name; }         Public Abstract voidAdd (Component c);  Public Abstract voidRemove (Component c);  Public Abstract voidDisplay (intdepth);}//Leaf : Represents a leaf node object. Leaf nodes have no child nodes. classLeafextendsComponent { PublicLeaf (String name) {Super(name); } @Override Public voidAdd (Component c) {System.out.println ("Can not add to a leaf"); } @Override Public voidRemove (Component c) {System.out.println ("Can not remove from a leaf"); } @Override Public voidDisplay (intdepth) {String temp= "";  for(inti = 0; I < depth; i++) {Temp+ = '-'; } System.out.println (Temp+name); }    }//Composite: Defines the behavior of the sub-point, which is used to store the child parts and implement the operations associated with the subassemblies in the Component interface. such as ADD and Remove. classCompositeextendsComponent {Privatelist<component> children =NewArraylist<component>();  PublicComposite (String name) {Super(name); } @Override Public voidAdd (Component c) {Children.add (c); } @Override Public voidRemove (Component c) {children.remove (c); } @Override Public voidDisplay (intdepth) {String temp= "";  for(inti = 0; I < depth; i++) {Temp+ = '-'; } System.out.println (Temp+name);  for(Component c:children) {c.display (depth+ 2); }    }}//Client: Manipulate objects in the structure through the Component interface.  Public classCompositepattern { Public Static voidMain (string[] args) {Composite root=NewComposite ("root"); Root. ADD (NewLeaf ("Leaf A"))); Root. ADD (NewLeaf ("Leaf B"))); Composite compx=NewComposite ("Composite X"); Compx.add (NewLeaf ("Leaf XA"))); Compx.add (NewLeaf ("Leaf XB"))); Root.        ADD (COMPX); Composite Compxy=NewComposite ("Composite XY"); Compxy.add (NewLeaf ("Leaf XYA"))); Compxy.add (NewLeaf ("Leaf XYB")));        Compx.add (COMPXY); Root. Display (1); }}

Applicable Scenarios

 1, want to represent the part of the object-the overall hierarchy.

2. If you want clients to ignore the difference between a combined object and a single object, the client will use all the objects in the composite structure uniformly.

Combined mode lets you optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures that make combinatorial patterns very useful.

A universal example of hierarchical data structures is what you encounter every time you use a computer: the file system. The file system consists of directories and files. Each directory can be loaded with content. The contents of a directory can be either a file or a directory. In this way, the computer's file system is organized in a recursive structure. If you want to describe such a data structure, you can use the combined pattern.

Java_ design Pattern _ Combined Mode _composite pattern (2016-08-12)

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.