add Composite Pattern

master
FengJungle 2019-10-24 08:32:55 +08:00
parent deca28dddb
commit bff93d45dc
6 changed files with 169 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -0,0 +1,116 @@
#ifndef __COMPOSITE_PATTERN_H__
#define __COMPOSITE_PATTERN_H__
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
//抽象构件
class Component
{
public:
Component(){}
Component(string iName){
this->name = iName;
}
//增加一个部门或办公室
virtual void add(Component*) = 0;
//撤销一个部门或办公室
virtual void remove(Component*) = 0;
//
virtual Component* getChild(int) = 0;
//各部门操作
virtual void operation() = 0;
string getName(){
return name;
}
private:
string name;
};
//叶子构件:办公室
class Office :public Component
{
public:
Office(string iName){
this->name = iName;
}
Office(){}
void add(Component* c){
printf("not support!\n");
}
void remove(Component* c){
printf("not support!\n");
}
Component* getChild(int i){
printf("not support!\n");
return NULL;
}
private:
string name;
};
//叶子构件:行政办公室
class AdminOffice :public Office
{
public:
AdminOffice(string iName){
this->name = iName;
}
void operation(){
printf("-----Administration Office:%s\n", name.c_str());
}
private:
string name;
};
//叶子构件:教务办公室
class DeanOffice :public Office
{
public:
DeanOffice(string iName){
this->name = iName;
}
void operation(){
printf("-----Dean Office:%s\n", name.c_str());
}
private:
string name;
};
//容器构件SubComponent
class SubComponent :public Component
{
public:
SubComponent(string iName){
this->name = iName;
}
void add(Component *c){
componentList.push_back(c);
}
void remove(Component *c){
for (int i = 0; i < componentList.size(); i++){
if (componentList[i]->getName() == c->getName()){
componentList.erase(componentList.begin() + i);
break;
}
}
}
Component* getChild(int i){
return (Component*)componentList[i];
}
void operation(){
printf("%s\n", this->name.c_str());
for (int i = 0; i < componentList.size(); i++){
((Component*)componentList[i])->operation();
}
}
private:
string name;
//构件列表
vector<Component*>componentList;
};
#endif //__COMPOSITE_PATTERN_H__

View File

@ -0,0 +1,48 @@
#include <iostream>
#include "CompositePattern.h"
int main()
{
Component *head, *sichuanBranch, *cdBranch, *myBranch, *office1, *office2, *office3,
*office4, *office5, *office6, *office7, *office8;
head = new SubComponent("总部");
sichuanBranch = new SubComponent("四川分部");
office1 = new AdminOffice("行政办公室");
office2 = new DeanOffice("教务办公室");
cdBranch = new SubComponent("成都分部");
myBranch = new SubComponent("绵阳分部");
office3 = new AdminOffice("行政办公室");
office4 = new DeanOffice("教务办公室");
office5 = new AdminOffice("行政办公室");
office6 = new DeanOffice("教务办公室");
office7 = new AdminOffice("行政办公室");
office8 = new DeanOffice("教务办公室");
cdBranch->add(office5);
cdBranch->add(office6);
myBranch->add(office7);
myBranch->add(office8);
sichuanBranch->add(office3);
sichuanBranch->add(office4);
sichuanBranch->add(cdBranch);
sichuanBranch->add(myBranch);
head->add(office1);
head->add(office2);
head->add(sichuanBranch);
head->operation();
system("pause");
return 0;
}

View File

@ -43,4 +43,8 @@
11.设计模式(十一)——桥接模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102694306
博客地址https://blog.csdn.net/sinat_21107433/article/details/102694306
12.设计模式(十二)——组合模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102712738