C + + design pattern shallow knowledge combination mode

Source: Internet
Author: User
Combined Mode (Composite): Combines objects into a tree structure to represent the ' part-whole ' hierarchy. The combined mode makes the user consistent with the use of individual objects and composite objects.

When to use the combined mode:

When you find that a requirement is a structure that reflects part and whole hierarchy, and that you want users to be able to ignore the difference between a composite object and a single object, you should consider using the combined pattern when you use all the objects in the composite structure uniformly.

The following code uses a combination pattern to define a class hierarchy that contains the basic objects of the human resources and finance departments and the combined objects such as branch offices, office, and so on. Basic objects can be combined into more complex objects, and this combination of objects can be combined so that they are recursively recursive, and in the client code, any object used in the base object can be used together. Simply put, the combination pattern allows the customer to consistently use the composite structure and individual objects.

Pattern implementation:

[Code] #ifndef composite_h#define composite_h#include<iostream> #include <string> #include <list> Using namespace Std;class company{//friend class concretecompany;protected:string Name;public:company () {} C    Ompany (String St): Name (ST) {} virtual void Add (company * c) {};    virtual void Remove (company * c) {};    virtual void Display (int depth) {cout << name;}; virtual void Lineofduty () {};};    Class Concretecompany:p ublic company{List < company*> companys;public:concretecompany (string st);    void Add (company * c);    void Remove (company * c);    void Display (int depth); void Lineofduty ();}; Concretecompany::concretecompany (String st) {name = st; Companys.push_back (new company (ST));} void Concretecompany::add (company * c) {companys.push_back (c);} void Concretecompany::remove (company * c) {companys.remove (c);}    void Concretecompany::D isplay (int depth) {int n = depth;    while (n) {cout << "-";--n; } for each(company* var in companys)        {Var->display (depth + 2);    cout << Endl;        }}void Concretecompany::lineofduty () {for each (company * var in companys) {var->lineofduty ();    cout << Endl;    }}class hrdepartment:public company{public:hrdepartment (String st);    void Add (company * c);    void Remove (company * c);    void Display (int depth); void Lineofduty ();}; void Hrdepartment::add (company * c) {}hrdepartment::hrdepartment (string st) {name = st;}    void Hrdepartment::remove (company * c) {}void hrdepartment::D isplay (int depth) {int n = depth;    while (n) {cout << "-";--n; } cout << name;} void Hrdepartment::lineofduty () {cout << name << "Employee recruitment training management. \ n ";}    Class Financedepartment:p ublic company{public:financedepartment (string st) {name = st;}    void Add (company * c) {};    void Remove (company * c) {};    void Display (int depth); void Lineofduty ();}; void Financedepartment::D isplay (int depth) {int n = depth;    while (n) {cout << "-";--n; } cout << name;} void Financedepartment::lineofduty () {cout << name << "Company financial revenue and expenditure management. \ n ";} #endif

Client:

[Code] #include "Composite.h" int main () {    company* 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 ("Shanghai Huadong Branch");    Comp. ADD (New Hrdepartment ("East China Branch Human Resources department"));    Comp. ADD (New Financedepartment ("East China Branch Company Finance Department"));    Root->add (&comp);    Concretecompany comp1 ("Nanjing Office");    Comp1. ADD (New Hrdepartment ("Nanjing Office Human Resources Department"));    Comp1. ADD (New Financedepartment ("Nanjing Office Finance Department"));    Comp. ADD (&COMP1);    Concretecompany comp2 ("Hangzhou Office");    Comp2. ADD (New Hrdepartment ("Hangzhou Office Human Resources Department"));    Comp2. ADD (New Financedepartment ("Hangzhou Office Finance Department"));    Comp. ADD (&COMP2);    Root->display (1);    Root->lineofduty ();    Comp. Remove (&COMP1);    Root->display (1);    Root->lineofduty ();    return 0;}

The above is the C + + design mode of shallow knowledge of the content of the combination mode, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.