C++ 设计者模式——观察者模式(Observer)

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

之所以能够通知到参加游泳比赛的各个动物,是因为在游泳比赛中有个队列,在队列中放着参加比赛的动物;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值