Java design pattern--structural mode--combined mode

Source: Internet
Author: User

What is a combination of patterns, this needs to be studied, the individual think is a combination of various forms of a structure bar.

Combination mode:

  

1 Combination Mode2 Overview3Combines objects into a tree structure to represent a "partial-whole" hierarchy. "Composite makes the user consistent with the use of individual objects and composite objects. "4  5  6 Applicability71. You want to represent the part of the object--The overall hierarchy. 8 92. You want users to ignore the difference between a combined object and a single object, and the user will use all the objects in the composite structure uniformly. Ten  One  A participants -1. Component - declares an interface for an object in a composition.  the when appropriate, implements the default behavior for all classes of common interfaces.  - declares an interface for accessing and managing component subcomponents.  - Optionally, define an interface in the recursive structure to access a parent part and implement it where appropriate.  -  +2. Leaf - The leaf node object is represented in the composition, and the leaf node has no child nodes.  + defines the behavior of a node object in a composition.  A  at3. Composite - defines the behavior of those parts that have child parts.  - stores the child parts.  - implement operations related to subassemblies in the component interface.  -  -4. Client inAn object that manipulates a combined part through the component interface.

Test class:

1 Importjava.util.List;2  3  Public classTest {4      Public Static voidMain (string[] args) {5Employer PM =NewProjectmanager ("Project manager"));6Employer PA =NewProjectassistant ("Project assistant");7         //Employer Programmer1 = new Programmer ("Programmer One");8Employer Programmer2 =NewProgrammer ("Programmer two");9         TenPm.add (PA);//add a project assistant to the project manager OnePm.add (PROGRAMMER2);//add programmers to the project manager A          -List<employer> EMS =pm.getemployers (); -          for(Employer Em:ems) { the System.out.println (Em.getname ()); -         } -     } -}

1 Importjava.util.List;2  3  Public Abstract classEmployer {4 5     PrivateString name;6     7      Public voidsetName (String name) {8          This. Name =name;9     }Ten      One      PublicString GetName () { A         return  This. Name; -     } -      the      Public Abstract voidAdd (Employer employer); -      -      Public Abstract voidDelete (employer employer); -      +      PublicList<employer>employers; -      +      Public voidPrintinfo () { A System.out.println (name); at     } -      -      PublicList<employer>getemployers () { -         return  This. Employers; -     } -}

1 Importjava.util.ArrayList;2  3  Public classProjectmanagerextendsEmployer {4     5      PublicProjectmanager (String name) {6 setName (name);7Employers =NewArraylist<employer>();8     }9     Ten      Public voidAdd (Employer employer) { One employers.add (employer); A     } -  -      Public voidDelete (Employer employer) { the employers.remove (employer); -     } -}

1  Public classProjectassistantextendsEmployer {2 3      Publicprojectassistant (String name) {4 setName (name);5Employers =NULL;//Project Assistant, says no subordinates.6     }7 8      Public voidAdd (Employer employer) {9         Ten     } One  A      Public voidDelete (Employer employer) { -          -     } the}

1  Public classProgrammerextendsEmployer {2 3      PublicProgrammer (String name) {4 setName (name);5Employers =NULL;//programmer, means no subordinates.6     }7 8      Public voidAdd (Employer employer) {9         Ten     } One  A      Public voidDelete (Employer employer) { -          -     } the}

Today I have studied ha ...

Java design pattern--structural mode--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.