The combination mode of design pattern

Source: Internet
Author: User

Combination mode

Combines objects into a tree structure to represent a partial overall hierarchy. The combined mode makes the user consistent with the use of individual objects and composite objects.

Component
 Packagecom.hml.combination; Public Abstract classComponent {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicComponent (String name) { This. Name =name; }         Public Abstract voidAdd (Component c);  Public Abstract voidRemove (Component c);  Public Abstract voidDisplayintdepth);}
Composite
 Packagecom.hml.combination;Importjava.util.ArrayList;Importjava.util.List; Public 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 voidDisplayintdepth) {         for(inti = 0; I < children.size (); i++) {children.get (i). display (i); }    }}
Leaf
 Packagecom.hml.combination; Public classLeafextendsComponent { PublicLeaf (String name) {Super(name); } @Override Public voidAdd (Component c) {System.out.println ("Can ' t add to a leaf"); } @Override Public voidRemove (Component c) {System.out.println ("Can ' t remove from a leaf"); } @Override Public voidDisplayintdepth) {System.out.println ("-" + depth + This. GetName ()); }}
Test
 Packagecom.hml.combination; Public classTest { Public Static voidMain (string[] args) {Composite root=NewComposite ("root"); Root.add (NewLeaf ("A")); Root.add (NewLeaf ("B")); Composite COM1=NewComposite ("COM1"); Com1.add (NewLeaf ("C")); Com1.add (NewLeaf ("D"));        Root.add (COM1); Composite COM2=NewComposite ("COM1"); Com2.add (NewLeaf ("E")); Com2.add (NewLeaf ("F"));        Root.add (COM2); Root.display (1); }}
Class diagram

The leaf has the add and remove methods, which is called transparent, which means that all methods used to manage child objects, including add and remove, are declared in component. All subclasses of the real component interface have add and remove. The advantage is that leaf nodes and minor points are indistinguishable from the outside world, and they have exactly the same behavior interface. But the problem is that implementing extra add and remove doesn't make any sense. In a secure manner, that is, the component interface does not declare the Add and remove methods, then the subclass leaf does not need to implement them, and in composite declares all the methods used to manage the subclass objects, so that does not appear the problem just mentioned, but because of lack of transparency, So leaves and branches of the class will not have the same interface, the client calls need to do the corresponding judgment, brought inconvenience.

Combining patterns should be considered when the requirement is to embody parts and the overall hierarchy, and if you want customers to be able to ignore the difference between a combined object and a single object, using all objects in a composite structure uniformly.

Combined mode of design 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.