//1.意图
/*
定义对象建的一种一对多的依赖关系,当一个对象的状态发生变化时,所依赖于他的对象得到通知并被自动更新;
2、别名
依赖、发布-订阅
*/
以动物参加比赛为列:
当比赛开始后,各个参赛选手都将通知,并且进行比赛;
参赛的动物分别是Dog,Cat,Penguin;
所有动物的父类:pet.h
#pragma once
#include<string>
#include<iostream>
using namespace std;
class CPet
{
public:
CPet(string petname)
{
m_petname = petname;
}
protected:
string m_petname;
};
//Cat.h
#pragma once
#include"pet.h"
#include"IClibTree.h"
#include"IRun.h"
class Cat :public CPet,public CIClibTree,public CIRun
{
public:
Cat(string catname) :CPet(catname)
{}
public:
void ClibTree(int length)
{
cout << this->m_petname << " Start ClibTree " << length << "match" << endl;
}
void Run(int length)
{
cout << this->m_petname << " Start Run " << length << "match" << endl;
}
};
//Dog.h
#pragma once
#include"pet.h"
#include"ISwim.h"
#include"IRun.h"
class Dog :public CPet,public CISwim,public CIRun
{
public :
Dog(string petname) :CPet(petname)
{}
void swim(int length)
{
cout << this->m_petname << " Start Swim " << length << "match" << endl;
}
void Run(int length)
{
cout << this->m_petname << " Start Run " << length << "match" << endl;
}
};
// penguin.h
#pragma once
#include"pet.h"
#include"ISwim.h"
#include"IRun.h"
class CPenguin:public CPet,public CISwim,public CIRun
{
public :
CPenguin(string petname) :CPet(petname)
{}
public:
void swim(int length)
{
cout << this->m_petname << " Start swim " << length << "match " << endl;
}
void Run(int length)
{
cout << this->m_petname << " Start Run " << length << "match " << endl;
}
};
比赛项目的接口:
IRun,IClibTree,ISwim
//IRun.h
#pragma once
class CIRun
{
public:
virtual void Run(int length)=0;
};
//IClibTree.h
#pragma once
class CIClibTree
{
public:
virtual void ClibTree(int length) =0;
};
//ISwim.h
#pragma once
class CISwim
{
public:
virtual void swim(int length)=0;
};
每个动物拥有哪些能力就继承哪些运动项目的接口,这样可以在检录的时候,如果某个动物不具备此项运动能力就不允许在这个游戏项目检录。
游戏项目父类:
#pragma once
#include<iostream>
#include<list>
#include<string>
#include"pet.h"
#include"ISwim.h"
using namespace std;
class CGame
{
public:
CGame(string gamename)
{
m_GameName = gamename;
}
~CGame() {}
protected:
string m_GameName;
};
游泳项目:继承游戏类
#pragma once
#include<list>
#include<iostream>
#include"Game.h"
#include"pet.h"
using namespace std;
class CSwimGame :public CGame
{
public:
CSwimGame(string gamename) :CGame(gamename)
{}
public:
void attach(CISwim* pet)
{
m_pets.push_back(pet);
}
void detach()
{
m_pets.pop_back();
}
//notify
//检录
void Check()
{
cout << "begin check" << endl;
}
//开始游戏
void StartGame(int length)
{
for (list<CISwim*>::iterator it = m_pets.begin(); it != m_pets.end(); ++it)
{
(*it)->swim(100);
}
}
private:
list<CISwim*> m_pets;
};
测试:
先构造各个动物,然后具备游泳能力的动物参加检录,当游戏开始后,各个动物都会被通知到,并且开始游泳;
#include"Dog.h"
#include"Cat.h"
#include"Penguin.h"
#include"SwimGame.h"
//class A
//{
//public:
// virtual void Print() =0;
//};
//class B :public A
//{
//public:
// void Print()
// {
// cout << "B Print" << endl;
// }
//};
int main()
{
//B lpB;
//A* lpA = &lpB;
//lpA->Print();
Dog d1("d1");
Dog d2("d2");
Cat c1("c1");
Cat c2("c2");
CPenguin p1("p1");
CPenguin p2("p2");
CSwimGame swim("swim");
//注册观察者
swim.attach(&d1);
swim.attach(&d2);
/*swim.attach(&c1);
swim.attach(&c2);*/
swim.attach(&p1);
swim.attach(&p2);
//notify
swim.Check();
swim.StartGame(10);
return 0;
}
之所以能够通知到参加游泳比赛的各个动物,是因为在游泳比赛中有个队列,在队列中放着参加比赛的动物;