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