diff --git a/13.ProxyPattern/1.Picture/代理模式UML图.png b/13.ProxyPattern/1.Picture/代理模式UML图.png new file mode 100644 index 0000000..de7ec52 Binary files /dev/null and b/13.ProxyPattern/1.Picture/代理模式UML图.png differ diff --git a/13.ProxyPattern/1.Picture/代理模式实例UML图.png b/13.ProxyPattern/1.Picture/代理模式实例UML图.png new file mode 100644 index 0000000..a2ad698 Binary files /dev/null and b/13.ProxyPattern/1.Picture/代理模式实例UML图.png differ diff --git a/13.ProxyPattern/2.Code/ProxyPattern.h b/13.ProxyPattern/2.Code/ProxyPattern.h new file mode 100644 index 0000000..b2e05b2 --- /dev/null +++ b/13.ProxyPattern/2.Code/ProxyPattern.h @@ -0,0 +1,64 @@ +#ifndef __FLYPATTERN_PATTERN_H__ +#define __FLYPATTERN_PATTERN_H__ + +#include +#include +using namespace std; + +// ɫ +class Subject +{ +public: + Subject(){} + virtual void method() = 0; +}; + +// ʵɫ +class RealSubject :public Subject +{ +public: + RealSubject(){} + void method(){ + printf("ҵ񷽷\n"); + } +}; + +// Log +class Log +{ +public: + Log(){} + string getTime(){ + time_t t = time(NULL); + char ch[64] = { 0 }; + //-- ʱ:: + strftime(ch, sizeof(ch)-1, "%Y-%m-%d %H:%M:%S", localtime(&t)); + return ch; + } +}; + +// +class Proxy:public Subject +{ +public: + Proxy(){ + realSubject = new RealSubject(); + log = new Log(); + } + void preCallMethod(){ + printf("method()ãʱΪ%s\n",log->getTime().c_str()); + } + void method(){ + preCallMethod(); + realSubject->method(); + postCallMethod(); + } + void postCallMethod(){ + printf("method()õóɹ!\n"); + } +private: + RealSubject *realSubject; + Log* log; +}; + +#endif //__FLYPATTERN_PATTERN_H__ \ No newline at end of file diff --git a/13.ProxyPattern/2.Code/main.cpp b/13.ProxyPattern/2.Code/main.cpp new file mode 100644 index 0000000..132e87e --- /dev/null +++ b/13.ProxyPattern/2.Code/main.cpp @@ -0,0 +1,14 @@ +#include +#include "ProxyPattern.h" + +int main() +{ + Subject *subject; + subject = new Proxy(); + subject->method(); + + printf("\n\n"); + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/14.ChainOfResponsibility/1.Picture/职责链模式UML图.png b/14.ChainOfResponsibility/1.Picture/职责链模式UML图.png new file mode 100644 index 0000000..7e8f0dd Binary files /dev/null and b/14.ChainOfResponsibility/1.Picture/职责链模式UML图.png differ diff --git a/14.ChainOfResponsibility/1.Picture/职责链模式UML实例图.png b/14.ChainOfResponsibility/1.Picture/职责链模式UML实例图.png new file mode 100644 index 0000000..c2299ef Binary files /dev/null and b/14.ChainOfResponsibility/1.Picture/职责链模式UML实例图.png differ diff --git a/14.ChainOfResponsibility/1.Picture/职责链模式举例图.png b/14.ChainOfResponsibility/1.Picture/职责链模式举例图.png new file mode 100644 index 0000000..53aaade Binary files /dev/null and b/14.ChainOfResponsibility/1.Picture/职责链模式举例图.png differ diff --git a/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h new file mode 100644 index 0000000..99978a0 --- /dev/null +++ b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h @@ -0,0 +1,135 @@ +#ifndef __CHAIN_OF_RESPONSIBILITY_PATTERN_H__ +#define __CHAIN_OF_RESPONSIBILITY_PATTERN_H__ + +#include +#include +using namespace std; + +// Ʊ +class Bill +{ +public: + Bill(){} + Bill(int iId, string iName, double iAccount){ + id = iId; + name = iName; + account = iAccount; + } + double getAccount(){ + return this->account; + } + void print(){ + printf("\nID:\t%d\n", id); + printf("Name:\t%s\n", name.c_str()); + printf("Account:\t%f\n", account); + } +private: + int id; + string name; + double account; +}; +// +class Approver +{ +public: + Approver(){} + Approver(string iName){ + setName(iName); + } + // ϼ + void setSuperior(Approver *iSuperior){ + this->superior = iSuperior; + } + // + virtual void handleRequest(Bill*) = 0; + string getName(){ + return name; + } + void setName(string iName){ + name = iName; + } +protected: + Approver *superior; +private: + string name; +}; + +// 崦ߣ鳤 +class GroupLeader :public Approver +{ +public: + GroupLeader(){} + GroupLeader(string iName){ + setName(iName); + } + // + void handleRequest(Bill *bill){ + if (bill->getAccount() < 10){ + printf("鳤 %s ˸ƱݣƱϢ",this->getName().c_str()); + bill->print(); + } + else{ + printf("鳤Ȩתϼ\n"); + this->superior->handleRequest(bill); + } + } +}; + +// 崦ߣ +class Head :public Approver +{ +public: + Head(){} + Head(string iName){ + setName(iName); + } + // + void handleRequest(Bill *bill){ + if (bill->getAccount() >= 10 && bill->getAccount()<30){ + printf(" %s ˸ƱݣƱϢ", this->getName().c_str()); + bill->print(); + } + else{ + printf("Ȩתϼ\n"); + this->superior->handleRequest(bill); + } + } +}; + +// 崦ߣ +class Manager :public Approver +{ +public: + Manager(){} + Manager(string iName){ + setName(iName); + } + // + void handleRequest(Bill *bill){ + if (bill->getAccount() >= 30 && bill->getAccount()<60){ + printf(" %s ˸ƱݣƱϢ", this->getName().c_str()); + bill->print(); + } + else{ + printf("Ȩתϼ\n"); + this->superior->handleRequest(bill); + } + } +}; + +// 崦ߣϰ +class Boss :public Approver +{ +public: + Boss(){} + Boss(string iName){ + setName(iName); + } + // + void handleRequest(Bill *bill){ + printf("ϰ %s ˸ƱݣƱϢ", this->getName().c_str()); + bill->print(); + } +}; + +#endif //__CHAIN_OF_RESPONSIBILITY_PATTERN_H__ \ No newline at end of file diff --git a/14.ChainOfResponsibility/2.Code/main.cpp b/14.ChainOfResponsibility/2.Code/main.cpp new file mode 100644 index 0000000..fc5bf9f --- /dev/null +++ b/14.ChainOfResponsibility/2.Code/main.cpp @@ -0,0 +1,33 @@ +#include +#include "ChainOfResponsibility.h" + +int main() +{ + // ߣ鳤磬ܣϰ + Approver *zuzhang, *bingge, *chunzong, *laoban; + + zuzhang = new GroupLeader(""); + bingge = new Head(""); + chunzong = new Manager(""); + laoban = new Boss("ϰ"); + + zuzhang->setSuperior(bingge); + bingge->setSuperior(chunzong); + chunzong->setSuperior(laoban); + + // + Bill *bill1 = new Bill(1, "Jungle", 8); + Bill *bill2 = new Bill(2, "Lucy", 14.4); + Bill *bill3 = new Bill(3, "Jack", 32.9); + Bill *bill4 = new Bill(4, "Tom", 89); + + // ȫȽ鳤 + zuzhang->handleRequest(bill1); printf("\n"); + zuzhang->handleRequest(bill2); printf("\n"); + zuzhang->handleRequest(bill3); printf("\n"); + zuzhang->handleRequest(bill4); + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index fa8b6da..3ff1046 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # DesignPattern -前言:Jungle设计模式系列 +Jungle设计模式系列 + +代码资源:https://github.com/FengJungle/DesignPattern 01.设计模式——设计模式概述 @@ -59,4 +61,12 @@ 15.设计模式(十五)——享元模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102763849 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102763849 + +15.设计模式(十六)——代理模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102772697 + +16.设计模式(十七)——职责链模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102790445 \ No newline at end of file