大话设计模式--组合模式 Composite -- C++实现实例

1. 组合模式: 将对象组合成树形结构以表示”部分--整体“的层次结构,组合模式使用户对单个对象和组合对象的使用具有一致性。

 

需求中是体现部分与整体层次的结构时,希望用户可以忽略组合对象和单个对象的不同,统一地使用组合结构中的所有对象时,就可以用组合模式。

 

实例:

company.h company.cpp 

#ifndef COMPANY_H
#define COMPANY_H

#include <QString>

class Company
{
public:
    Company(QString name);
    void virtual add(Company *c)=0;
    void virtual remove(Company *c)=0;
    void virtual display(QString depth)=0;
    void virtual lineOfDuty()=0;

protected:
    QString name;
};

#endif // COMPANY_H
#include "company.h"

Company::Company(QString name)
{
    this->name = name;
}


concretecompany.h concretecompany.cpp

#ifndef CONCRETECOMPANY_H
#define CONCRETECOMPANY_H

#include "company.h"
#include <QString>
#include <QList>

class ConcreteCompany : public Company
{
public:
    ConcreteCompany(QString name);
    void add(Company *c);
    void remove(Company *c);
    void display(QString depth);
    void lineOfDuty();

protected:
    QList<Company *> *children;
};

#endif // CONCRETECOMPANY_H
#include "concretecompany.h"
#include <QDebug>

ConcreteCompany::ConcreteCompany(QString name) : Company(name)
{
    children = new QList<Company *>();
}

void ConcreteCompany::add(Company *c)
{
    children->push_back(c);
}

void ConcreteCompany::remove(Company *c)
{
    children->removeOne(c);
}

void ConcreteCompany::display(QString depth)
{
    qDebug() << depth << name;
    for(int i=0; i!=children->count(); i++)
    {
        children->at(i)->display( depth + "--");
    }
}

void ConcreteCompany::lineOfDuty()
{
    for(int i=0; i!=children->count(); i++)
    {
        children->at(i)->lineOfDuty();
    }
}


financedepartment.h financedepartment.cpp 财务

#ifndef FINANCEDEPARTMENT_H
#define FINANCEDEPARTMENT_H

#include "company.h"
#include <QString>

class FinanceDepartment : public Company
{
public:
    FinanceDepartment(QString name);
    void add(Company *c);
    void remove(Company *c);
    void display(QString depth);
    void lineOfDuty();
};

#endif // FINANCEDEPARTMENT_H
#include "financedepartment.h"
#include <QDebug>

FinanceDepartment::FinanceDepartment(QString name) : Company(name)
{
}

void FinanceDepartment::add(Company *c)
{}

void FinanceDepartment::remove(Company *c)
{}

void FinanceDepartment::display(QString depth)
{
    qDebug() << depth << name;
}

void FinanceDepartment::lineOfDuty()
{
    qDebug() << "lineOfDuty, money";
}


hrdepartment.h hrdepartment.cpp    HR

#ifndef HRDEPARTMENT_H
#define HRDEPARTMENT_H

#include "company.h"
#include <QString>

class HRDepartment : public Company
{
public:
    HRDepartment(QString name);
    void add(Company *c);
    void remove(Company *c);
    void display(QString depth);
    void lineOfDuty();
};

#endif // HRDEPARTMENT_H
#include "hrdepartment.h"
#include <QDebug>

HRDepartment::HRDepartment(QString name) : Company(name)
{
}

void HRDepartment::add(Company *c)
{}

void HRDepartment::remove(Company *c)
{}

void HRDepartment::display(QString depth)
{
    qDebug() << depth << name;
}

void HRDepartment::lineOfDuty()
{
    qDebug() << "lineOfDuty, HR";
}


main.cpp

#include <QCoreApplication>
#include "concretecompany.h"
#include "hrdepartment.h"
#include "financedepartment.h"

int main(int argc, char *argv[])
{
    ConcreteCompany *root = new ConcreteCompany("beijing_company");
    root->add(new HRDepartment("beijing_HR"));
    root->add(new FinanceDepartment("beijing_money"));

    ConcreteCompany *sub = new ConcreteCompany("hangzhou_company");
    sub->add(new HRDepartment("hangzhou_HR"));
    sub->add(new FinanceDepartment("hangzhou_money"));
    root->add(sub);

    root->display("--");
}



 




 

 

 

posted @ 2013-10-15 15:20  今晚打酱油_  阅读(259)  评论(0编辑  收藏  举报