Home >Backend Development >C#.Net Tutorial >A brief introduction to the combination pattern of C++ design patterns

A brief introduction to the combination pattern of C++ design patterns

黄舟
黄舟Original
2017-01-17 13:28:511262browse

Composite mode (Composite): Combine objects into a tree structure to represent the 'part-whole' hierarchy. The composition pattern allows users to use single objects and composite objects consistently.

When to use the combination pattern:

When you find that the requirements reflect the hierarchical structure of parts and wholes, and you hope that users can ignore the differences between combined objects and single objects, and use them uniformly When combining all objects in a structure, you should consider using the Compose pattern.

The following code uses the combination pattern to define a class hierarchy containing basic objects such as the Human Resources Department and the Finance Department and combined objects such as branches and offices. Basic objects can be combined into more complex combined objects, and this combined object can be combined again, and so on. In the client code, combined objects can be used wherever basic objects are used. Simply put, the composition pattern allows clients to consistently use composite structures 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(){}
    Company(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 :public 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::Display(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::Display(int depth)
{
    int n = depth;
    while (n)
    {
        cout << "-"; --n;
    }
    cout << name;
}
void HRDepartment::LineOfDuty()
{
    cout << name << " 员工招聘培训管理。\n";
}

class FinanceDepartment :public Company
{
public:
    FinanceDepartment(string st) { name = st; }
    void Add(Company * c){};
    void Remove(Company * c){};
    void Display(int depth);
    void LineOfDuty();
};

void FinanceDepartment::Display(int depth)
{
    int n = depth;
    while (n)
    {
        cout << "-"; --n;
    }
    cout << name;
}

void FinanceDepartment::LineOfDuty()
{
    cout << name << " 公司财务收支管理。\n";
}

#endif

Client:

[code]#include"Composite.h"
int main()
{
    Company* root=new ConcreteCompany ("北京总公司");
    root->Add(new HRDepartment("总公司人力资源部"));
    root->Add(new FinanceDepartment("总公司财务部"));

    ConcreteCompany comp("上海华东分公司");
    comp.Add(new HRDepartment("华东分公司人力资源部"));
    comp.Add(new FinanceDepartment("华东分公司财务部"));
    root->Add(&comp);

    ConcreteCompany comp1("南京办事处");
    comp1.Add(new HRDepartment("南京办事处人力资源部"));
    comp1.Add(new FinanceDepartment("南京办事处财务部"));
    comp.Add(&comp1);

    ConcreteCompany comp2("杭州办事处");
    comp2.Add(new HRDepartment("杭州办事处人力资源部"));
    comp2.Add(new FinanceDepartment("杭州办事处财务部"));
    comp.Add(&comp2);
    root->Display(1);
    root->LineOfDuty();

    comp.Remove(&comp1);
    root->Display(1);
    root->LineOfDuty();
    return 0;
}

The above is the content of the C++ design pattern brief introduction to the combination pattern. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn