观察者模式参考博客:
推荐:https://www.cnblogs.com/yangjj08/p/10533250.html
https://blog.csdn.net/wuzhekai1985/article/details/6674984
单例模式
工厂模式
#include<iostream>
#include<list>
#include<string>
using namespace std;
/*
观察值模式:C++多态实现
一对多的依赖关系
一个博主建立的博客,该博客有很多粉丝,每个粉丝都是一个观察者;
博客内容更新时,会通知到每个粉丝,每个粉丝也会获得更新内容;
类 Blog->CSDN的派生关系,Blog类会定义一些函数和接口,子类CSDN会实现接口的功能;
类 People->Observer派生关系,
两个类之间,相互依赖,
Blog对象里有observer集合,Blog有更新时,调用observer集合中每个元素的上层接口(GetData函数),
observer对象里关联了Blog对象,当Blog对象有更新调用自己的上层接口告诉自己时,再通过这个Blog成员变量获取更新内容;
相当于沙特的石油涨价了,需告诉各国,具体内容很多,需各国自己去找人到沙特政府咨询;
沙特是一个Blog类,各国是个Observer类,通过成员变量的指针相互调用,
注意指针只是个地址,建立的对象才是实体内存空间;
两个类指针相互指向,通过公有函数通知和获取信息;
下面的代码实现了两个观察者模式,第二个的区别是在观察者构造自身时,绑定Blog,
*/
namespace Observer_space
{
class People
{
public:
People() { cout << "people constructor function\n"; }
~People() {}
virtual void GetData() {}
private:
};
class Blog
{
public:
Blog() { cout << "Blog constructor\n"; }
~Blog() {}
void AddPeople(People* p)
{
m_listPeople.push_back(p);
}
void RemovePeople(People* p)
{
m_listPeople.remove(p);
}
virtual void Notify()
{
for (auto it : m_listPeople)
{
it->GetData();
}
}
virtual void SetStatus(string str) { ms_status = str; }
virtual string GetStatus() { return ms_status; }
public:
string ms_status;
private:
list<People*> m_listPeople;
};
class CSDN :public Blog
{
public:
CSDN(string str)
:ms_blogName(str) {
cout << "CSDN constructor \n";
}
~CSDN() {}
void SetStatus(string str)
{
ms_status = "blog name:" + ms_blogName + ",change info:" + str;
}
private:
string ms_blogName;
};
class Observer :public People
{
public:
Observer(string str, Blog* blog)
:ms_name(str), m_blog(blog) {
cout << "observer constructor\n";
}
~Observer() {}
void GetData()
{
if (m_blog)
{
string str = m_blog->GetStatus();
cout << ms_name << " get info:\t " << str << endl;
}
}
private:
Blog* m_blog;
string ms_name;
};
}
int main_observer()
{
Observer_space::Blog* blog = new Observer_space::CSDN("up主");
Observer_space::People* zhangsan = new Observer_space::Observer("zhangsan",blog);
blog->AddPeople(zhangsan);
blog->SetStatus("change age");
blog->Notify();
delete blog;
delete zhangsan;
return 0;
}
//---------------------------------------
namespace Observer_space2 {
class Obs
{
public:
Obs() {}
~Obs() {}
virtual void GetInfo() = 0;
private:
};
class Subject
{
public:
Subject() {}
~Subject() {}
virtual string GetStatus() = 0;
virtual void SetStatus(string str) = 0;
virtual void Notify() {
for (auto it : ml_people)
{
it->GetInfo();
}
}
void AddObs(Obs* obs) {
if (obs)
ml_people.push_back(obs);
}
void Remove(Obs* obs)
{
if (obs)ml_people.remove(obs);
}
private:
list<Obs*> ml_people;
};
class Weibo :public Subject
{
public:
Weibo(string name) :ms_name(name) {}
~Weibo() {}
void SetStatus(string str) { ms_status = "weibo name:" + ms_name + " " + str; }
string GetStatus() { return ms_status; }
private:
string ms_status;
string ms_name;
};
class CObs :public Obs
{
public:
CObs(string name, Subject* sub) :
ms_name(name), m_subject(sub)
{
m_subject->AddObs(this);
}
~CObs() {}
void GetInfo()
{
cout << "observer name:" + ms_name + " " + m_subject->GetStatus() << endl;
}
private:
string ms_name;
Subject* m_subject;
};
}
int main()
{
Observer_space2::Subject* weibo = new Observer_space2::Weibo("长津湖");
Observer_space2::Obs* zhangsan = new Observer_space2::CObs("zhangsan", weibo);
Observer_space2::Obs* lisi = new Observer_space2::CObs("lisi", weibo);
weibo->SetStatus("age");
weibo->Notify();
delete weibo;
delete zhangsan;
delete lisi;
return 0;
}
/*输出结果
observer name:zhangsan weibo name:长津湖 age
observer name:lisi weibo name:长津湖 age
*/