From 3b7a291d5022100b475a6ae272490637341c6953 Mon Sep 17 00:00:00 2001 From: FengJungle Date: Wed, 6 Nov 2019 21:36:28 +0800 Subject: [PATCH] add Observer Pattern --- .../1.Picture/观察者模式UML图.png | Bin 0 -> 11068 bytes .../1.Picture/观察者模式实例UML图.png | Bin 0 -> 16150 bytes 20.ObserverPattern/2.Code/AllyCenter.cpp | 55 ++++++++++++++ 20.ObserverPattern/2.Code/AllyCenter.h | 36 ++++++++++ 20.ObserverPattern/2.Code/Demo.h | 67 ++++++++++++++++++ 20.ObserverPattern/2.Code/Observer.h | 59 +++++++++++++++ 20.ObserverPattern/2.Code/common.h | 10 +++ 20.ObserverPattern/2.Code/main.cpp | 32 +++++++++ README.md | 6 +- 9 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 20.ObserverPattern/1.Picture/观察者模式UML图.png create mode 100644 20.ObserverPattern/1.Picture/观察者模式实例UML图.png create mode 100644 20.ObserverPattern/2.Code/AllyCenter.cpp create mode 100644 20.ObserverPattern/2.Code/AllyCenter.h create mode 100644 20.ObserverPattern/2.Code/Demo.h create mode 100644 20.ObserverPattern/2.Code/Observer.h create mode 100644 20.ObserverPattern/2.Code/common.h create mode 100644 20.ObserverPattern/2.Code/main.cpp diff --git a/20.ObserverPattern/1.Picture/观察者模式UML图.png b/20.ObserverPattern/1.Picture/观察者模式UML图.png new file mode 100644 index 0000000000000000000000000000000000000000..e291c7329ee201de9ca0d88c591a001dde247d44 GIT binary patch literal 11068 zcmeHt_cvV8*Ed3xjOe`+ghUU6M2l$AqcduBhS7=WF**qm(M5DJq9i7|jNW@4VGzCd z?!EFn>wTWJ-e2B7;QPa@S@+&^&)w(jeLlNH3;&Wy(Zys$4bGoZ;o(Err0cLhsQ(_9t-fWJhWG2-R*<2XaGxige*B z#thMQrb=*aTQktAdEXPZhxK;iRd$6$33i3F++~2UekR~z_)}iH5#-enc=cxk;p#K7 zf>*cGu!fMyMj!@COAsX{=KG;oxU5*%1Z5wZ`BI9N5}0Q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b#HtG{>mrSj(Qko;S-syEVyn=05 zTebvx%>sbmBd#hSO`#OujrZ1vFN9g}8n$psAQD7vJVnN4?Cq!8b%@)cQPPE9t#^B+ zOE(7HU87;k5uF@Io`l~VcjitL{^E}!gBgvf0zRmHJm08)X0mWX)O=lq4(W1|)PDDH zn(6==GdC)0C^a8hL;-CsFRdjq0aqt%Y(B)P9IC+T83({{swEu}elI8$UVJKm*b94p zKN&i>eMX_J1^6$zz_FjdyZO3-pC1m71`p5$in6%XRN6(`AIeB+A9;3GcA6MU_OWEL zOd0qVvdP_XQJUyGV955(kjRT45A*c;MFMB1D$w%%f9q?Z3|HDeg^$u!W6hq{Kb7R| zs_A$?fXf6*Ki6KT1X5ju#h=SSyN}bBl)$XnkQtm z^mF;V)&V0+dod17W%uuketMxwW=wQ{Kgh4{2!7i8g#iVEH-Nc}^&k*sI6)4}t{|(6 z_3=r*@K17to9KRWqp8udr`H`YM!P3UmVnnyV^tz4Qvk#btt7_?Bj^jByft3U$c-w{aqoN3`kBbUTbYUMM_hIMW8t{)BWu|f;w@8QK6t|=0uWJ)% ycA{itfK%Z?Xh8B21L$Dy8E^xf6?fLdyrWBB3hlru7RW;ZeiUR>rRyZ$h5Qe6>6h~W literal 0 HcmV?d00001 diff --git a/20.ObserverPattern/2.Code/AllyCenter.cpp b/20.ObserverPattern/2.Code/AllyCenter.cpp new file mode 100644 index 0000000..4ce2978 --- /dev/null +++ b/20.ObserverPattern/2.Code/AllyCenter.cpp @@ -0,0 +1,55 @@ +#include "AllyCenter.h" +#include "Observer.h" + +/*********** AllyCenter ****************/ +AllyCenter::AllyCenter(){ + printf("󼪴Լ!\n"); +} + +// +void AllyCenter::join(Observer* player){ + if (playerList.size() == 4){ + printf("\n"); + return; + } + printf(" %s \n", player->getName().c_str()); + playerList.push_back(player); + if (playerList.size() == 4){ + printf("ӳɹҪˣһϣ\n"); + } +} +// Ƴ +void AllyCenter::remove(Observer* player){ + printf(" %s ˳\n", player->getName().c_str()); + //playerList.remove(player); +} +/*********** AllyCenter ****************/ + + +/********** AllyCenterController *******/ +AllyCenterController::AllyCenterController(){ + +} + +// ʵ֪ͨ +void AllyCenterController::notify(INFO_TYPE infoType, std::string name){ + switch (infoType){ + case RESOURCE: + for each (Observer* obs in playerList){ + if (obs->getName() != name){ + ((Player*)obs)->come(); + } + } + break; + case HELP: + for each (Observer* obs in playerList){ + if (obs->getName() != name){ + ((Player*)obs)->help(); + } + } + break; + default: + printf("Nothing\n"); + } +} +/********** AllyCenterController *******/ \ No newline at end of file diff --git a/20.ObserverPattern/2.Code/AllyCenter.h b/20.ObserverPattern/2.Code/AllyCenter.h new file mode 100644 index 0000000..12194b3 --- /dev/null +++ b/20.ObserverPattern/2.Code/AllyCenter.h @@ -0,0 +1,36 @@ +#ifndef __ALLYCENTER_H__ +#define __ALLYCENTER_H__ + +#include "common.h" +#include + +// ǰ +class Observer; +class Player; + +// Ŀ꣺ +class AllyCenter +{ +public: + AllyCenter(); + // ֪ͨ + virtual void notify(INFO_TYPE infoType, std::string name) = 0; + // + void join(Observer* player); + // Ƴ + void remove(Observer* player); +protected: + // б + std::vectorplayerList; +}; + +// Ŀ +class AllyCenterController :public AllyCenter +{ +public: + AllyCenterController(); + // ʵ֪ͨ + void notify(INFO_TYPE infoType, std::string name); +}; + +#endif //__ALLYCENTER_H__ \ No newline at end of file diff --git a/20.ObserverPattern/2.Code/Demo.h b/20.ObserverPattern/2.Code/Demo.h new file mode 100644 index 0000000..2fe284f --- /dev/null +++ b/20.ObserverPattern/2.Code/Demo.h @@ -0,0 +1,67 @@ +#define __DEMO_H__ +#ifdef __DEMO_H__ + +using namespace std; +#include + +// ۲ +class Observer +{ +public: + // Ӧ· + virtual void update() = 0; +}; + +// ۲ +class ConcreteObserver:public Observer +{ +public: + // ʵӦ· + void update(){ + // + } +}; + +// Ŀ +class Subject +{ +public: + // ӹ۲ + void attach(Observer* obs){ + obsList.push_back(obs); + } + // Ƴ۲ + void detach(Observer* obs){ + obsList.remove(obs); + } + // ֪ͨ + virtual void notify() = 0; +protected: + // ۲б + listobsList; +}; + +// Ŀ +class ConcreteSubject :public Subject +{ +public: + // ʵ֪ͨ + void notify(){ + // + // ֪ͨ۲߶ + for (int i = 0; i < obsList.size(); i++){ + obsList[i]->update(); + } + } +}; + +// ͻ˴ʾ +int main() +{ + Subject *sub = new ConcreteSubject(); + Observer *obs = new ConcreteObserver(); + sub->attach(obs); + sub->notify(); + return 0; +} +#endif \ No newline at end of file diff --git a/20.ObserverPattern/2.Code/Observer.h b/20.ObserverPattern/2.Code/Observer.h new file mode 100644 index 0000000..0c7e4bf --- /dev/null +++ b/20.ObserverPattern/2.Code/Observer.h @@ -0,0 +1,59 @@ +#ifndef __OBSERVER_H__ +#define __OBSERVER_H__ + +#include +using namespace std; +#include "common.h" +#include "AllyCenter.h" + +// ۲ Observer +class Observer +{ +public: + Observer(){} + // 󷽷 + virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0; + string getName(){ + return name; + } + void setName(string iName){ + this->name = iName; + } +private: + string name; +}; + +// ۲ +class Player :public Observer +{ +public: + Player(){ + setName("none"); + } + Player(string iName){ + setName(iName); + } + // ʵ + void call(INFO_TYPE infoType, AllyCenter* ac){ + switch (infoType){ + case RESOURCE: + printf("%s :\n", getName().c_str()); + break; + case HELP: + printf("%s :Ⱦ\n", getName().c_str()); + break; + default: + printf("Nothing\n"); + } + ac->notify(infoType, getName()); + } + // ʵ־巽 + void help(){ + printf("%s:ס㣡\n", getName().c_str()); + } + void come(){ + printf("%s:õģȡ\n", getName().c_str()); + } +}; + +#endif diff --git a/20.ObserverPattern/2.Code/common.h b/20.ObserverPattern/2.Code/common.h new file mode 100644 index 0000000..37d3212 --- /dev/null +++ b/20.ObserverPattern/2.Code/common.h @@ -0,0 +1,10 @@ +#ifndef __COMMON_H__ +#define __COMMON_H__ + +enum INFO_TYPE{ + NONE, + RESOURCE, + HELP +}; + +#endif //__COMMON_H__ \ No newline at end of file diff --git a/20.ObserverPattern/2.Code/main.cpp b/20.ObserverPattern/2.Code/main.cpp new file mode 100644 index 0000000..988458c --- /dev/null +++ b/20.ObserverPattern/2.Code/main.cpp @@ -0,0 +1,32 @@ +#include "Observer.h" +#include "AllyCenter.h" + +int main() +{ + // һս + AllyCenterController* controller = new AllyCenterController(); + + // 4ңս + Player* Jungle = new Player("Jungle"); + Player* Single = new Player("Single"); + Player* Jianmengtu = new Player(""); + Player* SillyDog = new Player("ɵӹ"); + controller->join(Jungle); + controller->join(Single); + controller->join(Jianmengtu); + controller->join(SillyDog); + + printf("\n\n"); + + // Jungleʣж + Jungle->call(RESOURCE, controller); + + printf("\n\n"); + + // ɵӹΣգȶ + SillyDog->call(HELP, controller); + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index d665246..ad640a6 100644 --- a/README.md +++ b/README.md @@ -89,4 +89,8 @@ Jungle设计模式系列 22.设计模式(二十二)——备忘录模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102907007 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102907007 + +23.设计模式(二十三)——观察者模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102927937 \ No newline at end of file