Design Mode-composite)

Source: Internet
Author: User
Combination Mode Overview
Combine objects into a tree structure to represent the "part-whole" hierarchy. "Composite makes the use of a single object and a Composite object consistent. "
Applicability
1. You want to represent the part of the object-the overall hierarchy. 2. You want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner.
Participants
1. Component is the object declaration interface in the combination. When appropriate, implement the default behavior of all class interfaces. Declare an interface to access and manage child components of Component. (Optional) define an interface in a recursive structure to access a parent component and implement it as appropriate. 2. Leaf indicates the Leaf node object in the combination. The Leaf node does not have any subnodes. Defines the behavior of node objects in a combination. 3. Composite defines the behavior of those parts with child parts. Storage Sub-parts. Perform operations related to sub-parts in the Component interface. 4. The Client uses the Component interface to manipulate the object of the Component. Example
Component public abstract class employer {private string name; Public void setname (string name) {This. name = Name;} Public String getname () {return this. name;} public abstract void add (Employer employer); public abstract void Delete (Employer employer); public list employers; Public void printinfo () {system. out. println (name);} public list getemployers () {return this. employers ;}} leaf public class programmer extends employer {public Programmer (string name) {setname (name); employers = NULL; // programmer, indicates no subordinates} public void add (Employer employer) {} public void Delete (Employer employer) {}} public class projectassistant extends employer {public projectassistant (string name) {setname (name); employers = NULL; // Project Assistant, indicating no subordinates} public void add (Employer employer) {} public void Delete (Employer employer) {}} composite public class projectmanager extends employer {public projectmanager (string name) {setname (name); employers = new arraylist ();} public void add (Employer employer) {employers. add (employer);} public void Delete (Employer employer) {employee. remove (Employer) ;}} client public class test {public static void main (string [] ARGs) {employer PM = new projectmanager ("Project Manager "); employer Pa = new projectassistant ("Project Assistant"); employer programmer1 = new Programmer ("Programmer 1"); employer programmer2 = new Programmer ("programmer 2"); PM. add (PA); // Add Project Assistant pm to the project manager. add (programmer2); // Add the programmer list EMS = PM for the project manager. getemployers (); For (Employer EM: EMS) {system. out. println (EM. getname () ;}} result project assistant programmer 2

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.