Combination of design patterns

Source: Internet
Author: User

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:



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.