2019-11-06 13:36:28 +00:00
|
|
|
#ifndef __OBSERVER_H__
|
|
|
|
#define __OBSERVER_H__
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
#include "common.h"
|
|
|
|
#include "AllyCenter.h"
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 抽象观察者 Observer
|
2019-11-06 13:36:28 +00:00
|
|
|
class Observer
|
|
|
|
{
|
|
|
|
public:
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual ~Observer(){}
|
2019-11-06 13:36:28 +00:00
|
|
|
Observer(){}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 声明抽象方法
|
2019-11-06 13:36:28 +00:00
|
|
|
virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
|
|
|
|
string getName(){
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
void setName(string iName){
|
|
|
|
this->name = iName;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
string name;
|
|
|
|
};
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
// 具体观察者
|
2019-11-06 13:36:28 +00:00
|
|
|
class Player :public Observer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Player(){
|
|
|
|
setName("none");
|
|
|
|
}
|
|
|
|
Player(string iName){
|
|
|
|
setName(iName);
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 实现
|
2019-11-06 13:36:28 +00:00
|
|
|
void call(INFO_TYPE infoType, AllyCenter* ac){
|
|
|
|
switch (infoType){
|
|
|
|
case RESOURCE:
|
2021-10-28 15:15:51 +00:00
|
|
|
printf("%s :我这里有物资\n", getName().c_str());
|
2019-11-06 13:36:28 +00:00
|
|
|
break;
|
|
|
|
case HELP:
|
2021-10-28 15:15:51 +00:00
|
|
|
printf("%s :救救我\n", getName().c_str());
|
2019-11-06 13:36:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Nothing\n");
|
|
|
|
}
|
|
|
|
ac->notify(infoType, getName());
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
// 实现具体方法
|
2019-11-06 13:36:28 +00:00
|
|
|
void help(){
|
2021-10-28 15:15:51 +00:00
|
|
|
printf("%s:坚持住,我来救你!\n", getName().c_str());
|
2019-11-06 13:36:28 +00:00
|
|
|
}
|
|
|
|
void come(){
|
2021-10-28 15:15:51 +00:00
|
|
|
printf("%s:好的,我来取物资\n", getName().c_str());
|
2019-11-06 13:36:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|