add Observer Pattern

master
FengJungle 2019-11-06 21:36:28 +08:00
parent ea62a3b182
commit 3b7a291d50
9 changed files with 264 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -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 *******/

View File

@ -0,0 +1,36 @@
#ifndef __ALLYCENTER_H__
#define __ALLYCENTER_H__
#include "common.h"
#include <vector>
// 前向声明
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::vector<Observer*>playerList;
};
// 具体目标
class AllyCenterController :public AllyCenter
{
public:
AllyCenterController();
// 实现通知方法
void notify(INFO_TYPE infoType, std::string name);
};
#endif //__ALLYCENTER_H__

View File

@ -0,0 +1,67 @@
#define __DEMO_H__
#ifdef __DEMO_H__
using namespace std;
#include <list>
// 抽象观察者
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:
// 观察者列表
list<Observer*>obsList;
};
// 具体目标
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

View File

@ -0,0 +1,59 @@
#ifndef __OBSERVER_H__
#define __OBSERVER_H__
#include <iostream>
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

View File

@ -0,0 +1,10 @@
#ifndef __COMMON_H__
#define __COMMON_H__
enum INFO_TYPE{
NONE,
RESOURCE,
HELP
};
#endif //__COMMON_H__

View File

@ -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;
}

View File

@ -89,4 +89,8 @@ Jungle设计模式系列
22.设计模式(二十二)——备忘录模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102907007
博客地址https://blog.csdn.net/sinat_21107433/article/details/102907007
23.设计模式(二十三)——观察者模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102927937