A concept
- Combined mode, combining objects in a tree structure to represent "part - whole 'hierarchy. Combined mode enables the user to use a single consistent combinations of objects and object.
Two UML diagrams
- Interface Component object declaration combination, where appropriate, any class that implements a total default behavior of the interface. Declare an interface for accessing and managing subcomponent of Component
- Leaf leaf nodes represent objects in combination, the leaf node has no children.
- Composite behavior is defined with the branch node, for storing sub-members, the sub-implemented operations related components Component interface, such as adding and deleting Remove Add
When using a combination of three patterns
- Demand is reflected part of the overall hierarchy structure, and you want the user can ignore the different combinations of objects with a single object, all objects using a combination of a unified structure, you should consider using a combination mode.
Four C ++ code to achieve
#include "pch.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;
//公司类 抽象类或接口
class Company
{
public:
Company() {}
Company(string str)
:name(str) {}
virtual void Add(Company* c) = 0; //增加
virtual void Remove(Company* c) = 0; //移除
virtual void Display(int depth) = 0; //显示
virtual void LineOfDuty() = 0; //履行职责
inline bool operator==(const Company& company) const
{
return this->name == company.name;
}
protected:
string name;
};
class ConcreteCompany : public Company
{
public:
ConcreteCompany(string name) : Company(name)
{
children = new list<Company*>;
}
void Add(Company* c) override
{
children->push_back(c);
}
void Remove(Company* c) override //删除子节点
{
for (list<Company*>::iterator it = children->begin(); it != children->end(); ++it)
{
if (*(*it) == *c)
{
children->erase(it);
break;
}
}
}
void Display(int depth) override //显示
{
for (int ix = 0; ix < depth; ++ix)
{
cout << "-";
}
cout << this->name << endl;
for (list<Company*>::iterator it = children->begin(); it != children->end(); ++it)
{
(*it)->Display(depth + 2);
}
}
virtual void LineOfDuty() override //履行职责
{
for (list<Company*>::iterator it = children->begin(); it != children->end(); ++it)
{
(*it)->LineOfDuty();
}
}
private:
list<Company*>* children;
};
//人力资源部 树叶节点
class HRDepartment : public Company
{
public:
HRDepartment(string str) : Company(str) {}
void Add(Company* c) override //增加
{
}
void Remove(Company* c) override //移除
{
}
void Display(int depth) override //显示
{
for (int ix = 0; ix < depth; ++ix)
{
cout << "-";
}
cout << name << endl;
}
void LineOfDuty() override //履行职责
{
cout << this->name << " 员工招聘培训管理" << endl;
}
};
class FinanceDepartment : public Company
{
public:
FinanceDepartment(string str) : Company(str) {}
void Add(Company* c) override //增加
{
}
void Remove(Company* c) override //移除
{
}
void Display(int depth) override //显示
{
for (int ix = 0; ix < depth; ++ix)
{
cout << "-";
}
cout << name << endl;
}
void LineOfDuty() override //履行职责
{
cout << this->name << " 公司财务收支管理" << endl;
}
};
int main()
{
Company* 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 << "\n结构图" << endl;
root->Display(1);
cout << "\n职责:" << endl;
root->LineOfDuty();
return 0;
}