c++实现观察者模式完整源代码
common.h
#ifndef __COMMON_H__
#define __COMMON_H__
enum INFO_TYPE{
NONE,
RESOURCE,
HELP
};
#endif //__COMMON_H__
AllyCenter.h
#ifndef __ALLYCENTER_H__
#define __ALLYCENTER_H__
#include "common.h"
#include <vector>
// 前向声明
class Observer;
class Player;
// 抽象目标:联盟中心
class AllyCenter
{
public:
AllyCenter();
virtual ~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__
Observer.h
#ifndef __OBSERVER_H__
#define __OBSERVER_H__
#include <iostream>
using namespace std;
#include "common.h"
#include "AllyCenter.h"
// 抽象观察者 Observer
class Observer
{
public:
virtual ~Observer(){}
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
AllyCenter.cpp
#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 *******/
main.cpp
#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");
delete controller;
delete Jungle;
delete Single;
delete Jianmengtu;
delete SillyDog;
controller = nullptr;
Jungle = nullptr;
Single = nullptr;
Jianmengtu = nullptr;
SillyDog = nullptr;
return 0;
}
该博文为原创文章,未经博主同意不得转载,如同意转载请注明博文出处
本文章博客地址:https://cplusplus.blog.csdn.net/article/details/125015203