组合模式
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;
}