The combination mode mainly uses the relationship between the whole and the part. For example, copying a word and copying a row of words on notepad means copying.
In an OA system, a company includes the human resources department and the Finance Department. Its subsidiaries also contain the human resources department and the Finance Department, which are structured in a tree, parallel management is not allowed.
The above is a typical example of using the combination mode.
Figure:
Code:
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 ("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); Console. writeLine ("\ n structure:"); root. display (1); Console. writeLine ("\ n responsibility"); root. lineOfDuty (); Console. read () ;}// Company class abstract class or interface 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} // The class ConcreteCompany of the tree branch node of the specific company class implementation interface: company {private List <Company> childred = new List <Company> (); public ConcreteCompany (string name): base (name) {} public override void Add (Company c) {// throw new NotImplementedException (); childred. add (c);} public override void Remove (Company c) {// throw new NotImplementedException (); childred. remove (c);} public override void Display (int depth) {// throw new NotImplementedException (); Console. writeLine (new string ('-', depth) + name); foreach (Company component in childred) {component. display (depth + 2) ;}}// perform public override void LineOfDuty () {// throw new NotImplementedException (); foreach (Company component in childred) {component. lineOfDuty () ;}}// human resources department and Finance Department leaf node class HRDepartment: Company {public HRDepartment (string name): base (name) {} public override void Add (Company c) {// throw new NotImplementedException ();} public override void Remove (Company c) {// throw new NotImplementedException ();} public override void Display (int depth) {// throw new NotImplementedException (); Console. writeLine (new String ('-', depth) + name);} public override void LineOfDuty () {// throw new NotImplementedException (); Console. writeLine ("{0} Employee Recruitment and Training Management", name) ;}} class FinanceDepartment: Company {public FinanceDepartment (string name): base (name) {} public override void Add (Company c) {// throw new NotImplementedException ();} public override void Remove (Company c) {// throw new NotImplementedException ();} public override void Display (int depth) {// throw new NotImplementedException (); Console. writeLine (new string ('-', depth) + name);} public override void LineOfDuty () {// throw new NotImplementedException (); Console. writeLine ("{0} company financial revenue and expenditure management", name );}}
Running result: