feat: feat: add Observer Pattern
This commit is contained in:
parent
8a876a756e
commit
8ea61d1bc8
@ -1,9 +1,9 @@
|
|||||||
/*******************************************************************
|
/*******************************************************************
|
||||||
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||||
* GitHub: https://github.com/XMuli
|
* GitHub: https://github.com/XMuli
|
||||||
* Description: 观察者模式 Observer Pattern
|
* Description: 观察者模式(Observer Pattern)
|
||||||
* Reference: 情景 → Subject 发送一个消息,然后所有的 Observer A、B、C
|
* 情景 → Subject 发送一个消息,然后所有的 Observer A、B、C
|
||||||
* 都有收到,并且对相同的消息,做出各自不同的操作响应
|
* 都有收到,并且对相同的消息,做出各自不同的操作响应
|
||||||
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
|
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
@ -74,26 +74,26 @@ public:
|
|||||||
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
|
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
//int main()
|
||||||
{
|
//{
|
||||||
AObserver* a = new AObserver();
|
// AObserver* a = new AObserver();
|
||||||
BObserver* b = new BObserver();
|
// BObserver* b = new BObserver();
|
||||||
CObserver* c = new CObserver();
|
// CObserver* c = new CObserver();
|
||||||
ConcreteSubject* concreSubject = new ConcreteSubject();
|
// ConcreteSubject* concreSubject = new ConcreteSubject();
|
||||||
|
//
|
||||||
concreSubject->addObsvr(a);
|
// concreSubject->addObsvr(a);
|
||||||
concreSubject->addObsvr(b);
|
// concreSubject->addObsvr(b);
|
||||||
concreSubject->addObsvr(c);
|
// concreSubject->addObsvr(c);
|
||||||
concreSubject->notify("messagebox 1");
|
// concreSubject->notify("messagebox 1");
|
||||||
|
//
|
||||||
concreSubject->removeObsvr(c);
|
// concreSubject->removeObsvr(c);
|
||||||
concreSubject->notify("messagebox 2");
|
// concreSubject->notify("messagebox 2");
|
||||||
|
//
|
||||||
//a->doSomeThing();
|
// //a->doSomeThing();
|
||||||
//b->doSomeThing();
|
// //b->doSomeThing();
|
||||||
//c->doSomeThing();
|
// //c->doSomeThing();
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
/*****************************打印结果*******************************
|
/*****************************打印结果*******************************
|
||||||
|
Loading…
Reference in New Issue
Block a user