今天介绍另一种模式
观察者模式是我们经常用的一个模式,比如在用wcf做服务的时候通知客户端做一些操作一般用设计者模式。
今天做一个订报纸的小例子来理解一下观察者模式 出版者 订阅者=观察者模式
用下边的图来说就是 人民日报 订阅者=观察者模式
只要是订阅了人民日报的人,有了新报纸就会送到订阅者那里去,
当你不想订的时候取消就不订阅就不会再收到报纸了。
下面我们就来用代码实现一下这个模式
代码语言:javascript复制 //报纸接口
public interface INewsPaper
{
//添加订阅者
void RegisterSubscriber(ISubScribe f_subScribe);
//取消订阅
void RemoveSubScriber(ISubScribe f_subScribe);
//发送报纸
void SendPaper();
}
//订阅者
public interface ISubScribe
{
//有新的报纸了就会被执行通知
void HasNewPaper();
}
//人民日报
public class PeopleNewsPaper : INewsPaper
{
private List<ISubScribe> subList = new List<ISubScribe>();
public void RegisterSubscriber(ISubScribe f_subScribe)
{
subList.Add(f_subScribe);
}
public void RemoveSubScriber(ISubScribe f_subScribe)
{
if (subList.IndexOf(f_subScribe) >= 0)
{
subList.Remove(f_subScribe);
}
}
//发报纸啦~~
public void SendPaper()
{
foreach (ISubScribe _sub in subList)
{
_sub.HasNewPaper();
}
}
}
public class subHuman : ISubScribe
{
//订阅者的名字
private string p_name;
public subHuman(string f_name)
{
p_name = f_name;
}
//告诉订阅者有新报纸了
public void HasNewPaper()
{
Console.WriteLine(p_name "!! 有新的报纸了,请查收!");
}
}
开始订订阅,和调用了
代码语言:javascript复制static void Main(string[] args)
{
PeopleNewsPaper _paper = new PeopleNewsPaper();
subHuman _XiaoMing = new subHuman("小明");
subHuman _ZhaoYun = new subHuman("赵云");
subHuman _LiuBei = new subHuman("刘备");
//小明订报
_paper.RegisterSubscriber(_XiaoMing);
//赵云订报
_paper.RegisterSubscriber(_ZhaoYun);
//刘备订报
_paper.RegisterSubscriber(_LiuBei);
//有新报纸了
_paper.SendPaper();
Console.WriteLine("---------------发完报纸了------------------");
//小明不想订了,取消报纸
_paper.RemoveSubScriber(_XiaoMing);
//又有新报纸了 就没有小明的报纸 了
_paper.SendPaper();
Console.ReadLine();
}
c 代码
代码语言:javascript复制#pragma once
#include <iostream>
//订阅者
class ISubScribe
{
public:
//有新的报纸了就会被执行通知
virtual void HasNewPaper() = 0;
virtual ~ISubScribe(){}
};
//报纸接口
class INewsPaper
{
public:
//添加订阅者
virtual void RegisterSubscriber(ISubScribe* f_subScribe) = 0;
//取消订阅
virtual void RemoveSubScriber(ISubScribe* f_subScribe) = 0;
//发送报纸
virtual void SendPaper() = 0;
virtual ~INewsPaper(){}
};
代码语言:javascript复制#pragma once
#include "inewspaper.h"
#include <iostream>
#include <vector>
using namespace std;
class PeopleNewsPaper :
public INewsPaper
{
public:
PeopleNewsPaper();
virtual ~PeopleNewsPaper();
//添加订阅者
void RegisterSubscriber(ISubScribe* f_subScribe);
//取消订阅
void RemoveSubScriber(ISubScribe* f_subScribe);
//发送报纸
void SendPaper();
private:
vector<ISubScribe*> subs;
};
#include "PeopleNewsPaper.h"
#include <algorithm>
void MyClearSub(ISubScribe* sub)
{
if (sub) {
delete sub;
sub = nullptr;
}
}
PeopleNewsPaper::PeopleNewsPaper()
{
}
PeopleNewsPaper::~PeopleNewsPaper()
{
if (!subs.empty()) {
for_each(subs.begin(), subs.end(), MyClearSub);
subs.clear();
}
}
//添加订阅者
void PeopleNewsPaper::RegisterSubscriber(ISubScribe* f_subScribe)
{
if (subs.empty()) {
subs.push_back(f_subScribe);
}
else if (find(subs.begin(), subs.end(), f_subScribe) == subs.end()) {
subs.push_back(f_subScribe);
}
}
//取消订阅
void PeopleNewsPaper::RemoveSubScriber(ISubScribe* f_subScribe)
{
vector<ISubScribe*>::iterator itor = find(subs.begin(), subs.end(), f_subScribe);
if (itor != subs.end()) {
subs.erase(itor);
MyClearSub(f_subScribe);
}
}
//发送报纸
void PeopleNewsPaper::SendPaper()
{
for (ISubScribe* sub : subs) {
sub->HasNewPaper();
}
}
代码语言:javascript复制#pragma once
#include "inewspaper.h"
#include <iostream>
#include <string>
class SubHuman :
public ISubScribe
{
public:
SubHuman(std::string name);
~SubHuman();
void HasNewPaper();
private:
std::string m_name;
};
#include "SubHuman.h"
using namespace std;
SubHuman::SubHuman(string name)
{
m_name = name;
}
SubHuman::~SubHuman()
{
}
void SubHuman::HasNewPaper()
{
std::cout << m_name << "有报纸来了!!" << endl;
}
调用
代码语言:javascript复制#include <iostream>
#include "inewspaper.h"
#include "PeopleNewsPaper.h"
#include "SubHuman.h"
int main()
{
INewsPaper* peopPaper = new PeopleNewsPaper();
ISubScribe* sub = new SubHuman("小李");
peopPaper->RegisterSubscriber(sub);
sub = new SubHuman("小张");
peopPaper->RegisterSubscriber(sub);
sub = new SubHuman("小刘");
peopPaper->RegisterSubscriber(sub);
peopPaper->SendPaper();
peopPaper->RemoveSubScriber(sub);
peopPaper->SendPaper();
delete peopPaper;
}