组合模式(大话设计模式)C/C++版本
组合模式
C++
#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
#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;
}
原文地址:https://blog.csdn.net/m0_47104421/article/details/140262030
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!