diff --git a/10.DecoratorPattern/1.Picture/装饰模式UML图.png b/10.DecoratorPattern/1.Picture/装饰模式UML图.png new file mode 100644 index 0000000..bfd7575 Binary files /dev/null and b/10.DecoratorPattern/1.Picture/装饰模式UML图.png differ diff --git a/10.DecoratorPattern/1.Picture/装饰模式实例UML图.png b/10.DecoratorPattern/1.Picture/装饰模式实例UML图.png new file mode 100644 index 0000000..dafcd6b Binary files /dev/null and b/10.DecoratorPattern/1.Picture/装饰模式实例UML图.png differ diff --git a/10.DecoratorPattern/1.Picture/装饰模式实例图.png b/10.DecoratorPattern/1.Picture/装饰模式实例图.png new file mode 100644 index 0000000..481a5b1 Binary files /dev/null and b/10.DecoratorPattern/1.Picture/装饰模式实例图.png differ diff --git a/10.DecoratorPattern/2.Code/DecoratorPattern.h b/10.DecoratorPattern/2.Code/DecoratorPattern.h new file mode 100644 index 0000000..861463b --- /dev/null +++ b/10.DecoratorPattern/2.Code/DecoratorPattern.h @@ -0,0 +1,93 @@ +#ifndef __DECORATOR_PATTERN_H__ +#define __DECORATOR_PATTERN_H__ + +//󹹼 +class Component +{ +public: + Component(){} + virtual void operation() = 0; +}; + +//幹 +class Phone :public Component +{ +public: + Phone(){} + void operation(){ + printf("ֻ\n"); + } +}; + +//װ +class Decorator :public Component +{ +public: + Decorator(){} + Decorator(Component *c){ + this->component = c; + } + void operation(){ + this->component->operation(); + } + Component *getComponent(){ + return this->component; + } + void setComponent(Component *c){ + this->component = c; + } +private: + Component *component; +}; + +//װֻࣺ +class DecoratorShell:public Decorator +{ +public: + DecoratorShell(){} + DecoratorShell(Component *c){ + this->setComponent(c); + } + void operation(){ + this->getComponent()->operation(); + this->newBehavior(); + } + void newBehavior(){ + printf("װֻ\n"); + } +}; + +//װֻֽࣺ +class DecoratorSticker :public Decorator +{ +public: + DecoratorSticker(){} + DecoratorSticker(Component *c){ + this->setComponent(c); + } + void operation(){ + this->getComponent()->operation(); + this->newBehavior(); + } + void newBehavior(){ + printf("ֽͨ\n"); + } +}; + +//װֻࣺ +class DecoratorRope :public Decorator +{ +public: + DecoratorRope(){} + DecoratorRope(Component *c){ + this->setComponent(c); + } + void operation(){ + this->getComponent()->operation(); + this->newBehavior(); + } + void newBehavior(){ + printf("ϵֻ\n"); + } +}; +#endif //__DECORATOR_PATTERN_H__ \ No newline at end of file diff --git a/10.DecoratorPattern/2.Code/main.cpp b/10.DecoratorPattern/2.Code/main.cpp new file mode 100644 index 0000000..b19f0f3 --- /dev/null +++ b/10.DecoratorPattern/2.Code/main.cpp @@ -0,0 +1,37 @@ +#include +#include "DecoratorPattern.h" + +int main() +{ + printf("\nJungleĵһֻ\n"); + Component *c; + Component *com_Shell; + c = new Phone(); + com_Shell = new DecoratorShell(c); + com_Shell->operation(); + + printf("\nJungleĵڶֻ\n"); + Component *c2; + Component *com_Shell2; + c2 = new Phone(); + com_Shell2 = new DecoratorShell(c2); + Component *com_Sticker; + com_Sticker = new DecoratorSticker(com_Shell2); + com_Sticker->operation(); + + printf("\nJungleĵֻ\n"); + Component *c3; + Component *com_Shell3; + c3 = new Phone(); + com_Shell3 = new DecoratorShell(c3); + Component *com_Sticker2; + com_Sticker2 = new DecoratorSticker(com_Shell3); + Component *com_Rope; + com_Rope = new DecoratorRope(com_Sticker2); + com_Rope->operation(); + + printf("\n\n"); + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 219b6e2..8b574c1 100644 --- a/README.md +++ b/README.md @@ -47,4 +47,8 @@ 12.设计模式(十二)——组合模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102712738 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102712738 + +13.设计模式(十三)——装饰模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102733023 \ No newline at end of file