diff --git a/9.CompositePattern/1.Picture/组合模式.png b/9.CompositePattern/1.Picture/组合模式.png new file mode 100644 index 0000000..46ec659 Binary files /dev/null and b/9.CompositePattern/1.Picture/组合模式.png differ diff --git a/9.CompositePattern/1.Picture/组合模式UML图.png b/9.CompositePattern/1.Picture/组合模式UML图.png new file mode 100644 index 0000000..a10f612 Binary files /dev/null and b/9.CompositePattern/1.Picture/组合模式UML图.png differ diff --git a/9.CompositePattern/1.Picture/组合模式实例图.png b/9.CompositePattern/1.Picture/组合模式实例图.png new file mode 100644 index 0000000..abeb36a Binary files /dev/null and b/9.CompositePattern/1.Picture/组合模式实例图.png differ diff --git a/9.CompositePattern/2.Code/CompositePattern.h b/9.CompositePattern/2.Code/CompositePattern.h new file mode 100644 index 0000000..9834c2e --- /dev/null +++ b/9.CompositePattern/2.Code/CompositePattern.h @@ -0,0 +1,116 @@ +#ifndef __COMPOSITE_PATTERN_H__ +#define __COMPOSITE_PATTERN_H__ + +#include +#include +#include +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; + + //б + vectorcomponentList; +}; + +#endif //__COMPOSITE_PATTERN_H__ \ No newline at end of file diff --git a/9.CompositePattern/2.Code/main.cpp b/9.CompositePattern/2.Code/main.cpp new file mode 100644 index 0000000..98c635f --- /dev/null +++ b/9.CompositePattern/2.Code/main.cpp @@ -0,0 +1,48 @@ +#include +#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; +} \ No newline at end of file diff --git a/README.md b/README.md index 4a86323..219b6e2 100644 --- a/README.md +++ b/README.md @@ -43,4 +43,8 @@ 11.设计模式(十一)——桥接模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102694306 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102694306 + +12.设计模式(十二)——组合模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102712738 \ No newline at end of file