I. Overview
Concept: Combine objects into a tree structure to represent a "partial whole" hierarchy. The combination mode makes the user consistent with the use of a single object.
Client: Client
Component: object interface declaration in composite mode. When appropriate, the default behavior of all class interfaces is implemented. Declare an interface to access and manage its child parts.
Submenu: indicates the Node object, but there are no subnodes.
Composite: defines the behavior of a branch node. It is used to store sub-parts and implement operations related to sub-parts in the component interface, such as adding and deleting.
Ii. Basic code (C #)
In C ++, STL --- list does not recognize and support abstract objects in the form of C ++ because it is different from C.
The abstract method declared by the base class cannot call the specific implementation of the subclass !!!
Using system; using system. collections. generic; using system. text; namespace combination mode {class program {static void main (string [] ARGs) {composite root = new composite ("root"); root. add (new leaf ("leaf A"); root. add (new leaf ("leaf B"); composite comp = new composite ("Composite X"); comp. add (new leaf ("leaf xa"); comp. add (new leaf ("leaf XB"); root. add (COMP); composite comp2 = new composite ("Composite XY"); comp2.add (new leaf ("leaf xya ")); comp2.add (new leaf ("leaf xyb"); comp. add (comp2); root. add (new leaf ("leaf C"); leaf = new leaf ("leaf D"); root. add (leaf); root. remove (leaf); root. display (1); console. read () ;}} abstract class component {protected string name; public component (string name) {This. name = Name;} public abstract void add (component C); public abstract void remove (component C); public abstract void display (INT depth);} class composite: component {private list <component> Children = new list <component> (); Public composite (string name): Base (name) {} public override void add (component C) {children. add (c);} public override void remove (component C) {children. remove (c);} public override void display (INT depth) {console. writeline (new string ('-', depth) + name); foreach (Component component in children) {component. display (depth + 2) ;}}} class leaf: component {public leaf (string name): Base (name) {} public override void add (component C) {console. writeline ("cannot add to a leaf");} public override void remove (component C) {console. writeline ("cannot remove from a leaf");} public override void display (INT depth) {console. writeline (new string ('-', depth) + name );}}}
3. The company's management system adopts a combination model
Using system; using system. collections. generic; using system. text; namespace combination mode {class program {static void main (string [] ARGs) {concretecompany root = new concretecompany ("Beijing Head Office"); root. add (New hrdepartment ("Head Office Human Resources Department"); root. add (New financedepartment ("Head Office Finance Department"); concretecompany comp = new concretecompany ("Shanghai East China Branch"); comp. add (New hrdepartment ("China East Branch Human Resources Department"); comp. add (New financedepartment ("Finance Department of China East Branch"); root. add (COMP); concretecompany comp1 = new concretecompany ("Nanjing Office"); comp1.add (New hrdepartment ("Nanjing Office Human Resources Department ")); comp1.add (New financedepartment ("Nanjing Office Finance Department"); comp. add (comp1); concretecompany comp2 = new concretecompany ("Hangzhou Office"); comp2.add (New hrdepartment ("Hangzhou Office Human Resources Department ")); comp2.add (New financedepartment ("Hangzhou Office Finance Department"); comp. add (comp2); console. writeline ("\ n structure:"); root. display (1); console. writeline ("\ n responsibility:"); root. lineofduty (); console. read () ;}abstract class company {protected string name; Public Company (string name) {This. name = Name;} public abstract void add (Company C); // Add public abstract void remove (Company C); // remove public abstract void display (INT depth ); // display public abstract void lineofduty (); // perform duties} class concretecompany: Company {private list <company> Children = new list <company> (); public concretecompany (string name): Base (name) {} public override void add (Company C) {children. add (c);} public override void remove (Company C) {children. remove (c);} public override void display (INT depth) {console. writeline (new string ('-', depth) + name); foreach (company component in children) {component. display (depth + 2) ;}}// perform public override void lineofduty () {foreach (company component in children) {component. lineofduty () ;}}// HR department class hrdepartment: Company {public hrdepartment (string name): Base (name) {} public override void add (Company C) {} public override void remove (Company C) {} public override void display (INT depth) {console. writeline (new string ('-', depth) + name);} public override void lineofduty () {console. writeline ("{0} Employee Recruitment and Training Management", name) ;}// Finance Department class financedepartment: Company {public financedepartment (string name): Base (name) {} public override void add (Company C) {} public override void remove (Company C) {} public override void display (INT depth) {console. writeline (new string ('-', depth) + name);} public override void lineofduty () {console. writeline ("{0} company financial revenue and expenditure management", name );}}}