组合模式(大话设计模式)C/C++版本

组合模式

C++

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;
// 公司类... 抽象或者接口
class Company
{
public:
    Company(string name)
    {
        this->name = name;
    }
    virtual void Add(Company *c) = 0;    // 增加
    virtual void Remove(Company *c) = 0; // 移除
    virtual void Display(int depth) = 0; // 显示
    virtual void LineOfDuty() = 0;       // 履行职责...不同部分履行的职责不一样
protected:
    string name;
};

// 具体公司类  树枝节点
class ConcreteCompany : public Company
{
public:
    ConcreteCompany(string name) : Company(name) {}

    virtual void Add(Company *c)
    {
        children->push_back(c);
    }
    virtual void Remove(Company *c)
    {
        children->remove(c);
    }
    virtual void Display(int depth)
    {
        string s(depth, '-');
        cout << s << name << endl;
        for (list<Company *>::iterator it = children->begin(); it != children->end(); it++)
        {
            (*it)->Display(depth + 2);
        }
    }
    virtual void LineOfDuty()
    {
        for (list<Company *>::iterator it = children->begin(); it != children->end(); it++)
        {
            (*it)->LineOfDuty();
        }
    }

private:
    list<Company *> *children = new list<Company *>;
};

// 叶子节点  人力资源部
class HRDepartment : public Company
{
public:
    HRDepartment(string name) : Company(name) {}

    virtual void Add(Company *c) {}
    virtual void Remove(Company *c) {}
    virtual void Display(int depth)
    {
        string s(depth, '-');
        cout << s << name << endl;
    }
    virtual void LineOfDuty()
    {
        cout << name << " 员工招聘培训管理" << endl;
    }
};

class FinanceDepartment : public Company
{
public:
    FinanceDepartment(string name) : Company(name) {}

    virtual void Add(Company *c) {}
    virtual void Remove(Company *c) {}
    virtual void Display(int depth)
    {
        string s(depth, '-');
        cout << s << name << endl;
    }
    virtual void LineOfDuty()
    {
        cout << name << " 公司财务收支管理" << endl;
    }
};

int main()
{
    ConcreteCompany *root = new ConcreteCompany("北京总公司");
    root->Add(new HRDepartment("总公司人力资源部"));
    root->Add(new FinanceDepartment("总公司财务部"));

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

    ConcreteCompany *comp1 = new ConcreteCompany("南京办事处");
    comp1->Add(new HRDepartment("南京办事处人力资源部"));
    comp1->Add(new FinanceDepartment("南京办事处财务部"));
    comp->Add(comp1);

    ConcreteCompany *comp2 = new ConcreteCompany("杭州办事处");
    comp2->Add(new HRDepartment("杭州办事处人力资源部"));
    comp2->Add(new FinanceDepartment("杭州办事处财务部"));
    comp->Add(comp2);

    cout << "结构图: " << endl;
    root->Display(1);

    cout << "\n职责图: " << endl;
    root->LineOfDuty();

    delete comp2;
    delete comp1;
    delete comp;
    delete root;

    return 0;
}

C

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Company
{
    char *name;
    struct Company **children;
    size_t child_count;
    void (*add)(struct Company *, struct Company *);
    void (*remove)(struct Company *);
    void (*display)(struct Company *, int);
    void (*lineOfDuty)(struct Company *);
} Company;

typedef struct
{
    Company company;
    void (*lineOfDuty)(Company *);
} LeafCompany;

void company_add(Company *company, Company *child);
void company_display(Company *company, int depth);
void company_lineOfDuty(Company *company);

void company_init(Company *company, const char *name)
{
    company->name = strdup(name);
    company->children = NULL;
    company->child_count = 0;
    company->add = &company_add;
    company->remove = NULL; // 实现时需要具体逻辑
    company->display = &company_display;
    company->lineOfDuty = &company_lineOfDuty;
}

void company_free(Company *company)
{
    free(company->name);
    size_t i;
    for (i = 0; i < company->child_count; ++i)
    {
        company_free(company->children[i]);
    }
    free(company->children);
    free(company);
}

void company_add(Company *company, Company *child)
{
    company->children = realloc(company->children, sizeof(Company *) * (company->child_count + 1));
    company->children[company->child_count++] = child;
}

void company_display(Company *company, int depth)
{
    char s[depth + 1];
    memset(s, '-', depth);
    s[depth] = '\0';
    printf("%s%s\n", s, company->name);
    size_t i;
    for (i = 0; i < company->child_count; ++i)
    {
        company->children[i]->display(company->children[i], depth + 2);
    }
}

void company_lineOfDuty(Company *company)
{
    size_t i;
    for (i = 0; i < company->child_count; ++i)
    {
        company->children[i]->lineOfDuty(company->children[i]);
    }
}

void leaf_company_init(LeafCompany *leaf, const char *name, void (*duty)(Company *))
{
    leaf->company.name = strdup(name);
    leaf->company.add = NULL;
    leaf->company.remove = NULL;
    leaf->company.display = &company_display;
    leaf->company.lineOfDuty = duty;
    leaf->lineOfDuty = duty;
}

void hr_department_lineOfDuty(Company *company)
{
    printf("%s 员工招聘培训管理\n", company->name);
}

void finance_department_lineOfDuty(Company *company)
{
    printf("%s 公司财务收支管理\n", company->name);
}

int main()
{
    Company *root = malloc(sizeof(Company));
    company_init(root, "北京总公司");

    LeafCompany *hr_root = malloc(sizeof(LeafCompany));
    leaf_company_init(hr_root, "总公司人力资源部", hr_department_lineOfDuty);
    root->add(root, (Company *)hr_root);

    LeafCompany *finance_root = malloc(sizeof(LeafCompany));
    leaf_company_init(finance_root, "总公司财务部", finance_department_lineOfDuty);
    root->add(root, (Company *)finance_root);

    Company *shanghai_comp = malloc(sizeof(Company));
    company_init(shanghai_comp, "上海华东分公司");
    root->add(root, shanghai_comp);

    LeafCompany *hr_shanghai = malloc(sizeof(LeafCompany));
    leaf_company_init(hr_shanghai, "华东分公司人力资源部", hr_department_lineOfDuty);
    shanghai_comp->add(shanghai_comp, (Company *)hr_shanghai);

    LeafCompany *finance_shanghai = malloc(sizeof(LeafCompany));
    leaf_company_init(finance_shanghai, "华东分公司财务部", finance_department_lineOfDuty);
    shanghai_comp->add(shanghai_comp, (Company *)finance_shanghai);

    Company *nanjing_office = malloc(sizeof(Company));
    company_init(nanjing_office, "南京办事处");
    shanghai_comp->add(shanghai_comp, nanjing_office);

    LeafCompany *hr_nanjing = malloc(sizeof(LeafCompany));
    leaf_company_init(hr_nanjing, "南京办事处人力资源部", hr_department_lineOfDuty);
    nanjing_office->add(nanjing_office, (Company *)hr_nanjing);

    LeafCompany *finance_nanjing = malloc(sizeof(LeafCompany));
    leaf_company_init(finance_nanjing, "南京办事处财务部", finance_department_lineOfDuty);
    nanjing_office->add(nanjing_office, (Company *)finance_nanjing);

    printf("结构图:\n");
    root->display(root, 1);

    printf("\n职责图:\n");
    root->lineOfDuty(root);

    company_free(root); // 释放根节点,递归释放所有子节点

    return 0;
}
相关推荐
WaaTong几秒前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
霁月风4 分钟前
设计模式——观察者模式
c++·观察者模式·设计模式
QAQ小菜鸟25 分钟前
一、初识C语言(1)
c语言
何曾参静谧40 分钟前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
互联网打工人no144 分钟前
每日一题——第一百二十一题
c语言
朱一头zcy2 小时前
C语言复习第9章 字符串/字符/内存函数
c语言
此生只爱蛋2 小时前
【手撕排序2】快速排序
c语言·c++·算法·排序算法
何曾参静谧3 小时前
「C/C++」C/C++ 指针篇 之 指针运算
c语言·开发语言·c++
暗黑起源喵3 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
lulu_gh_yu3 小时前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法