feat: feat: add Observer Pattern

This commit is contained in:
XMuli 2022-12-24 23:03:00 +08:00
parent 8a876a756e
commit 8ea61d1bc8
No known key found for this signature in database
GPG Key ID: 9554B5DD5B8E986A

View File

@ -1,9 +1,9 @@
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: Observer Pattern
* Reference: Subject Observer ABC
*
* Description: (Observer Pattern)
* Subject Observer ABC
*
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
******************************************************************/
@ -74,26 +74,26 @@ public:
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
};
int main()
{
AObserver* a = new AObserver();
BObserver* b = new BObserver();
CObserver* c = new CObserver();
ConcreteSubject* concreSubject = new ConcreteSubject();
concreSubject->addObsvr(a);
concreSubject->addObsvr(b);
concreSubject->addObsvr(c);
concreSubject->notify("messagebox 1");
concreSubject->removeObsvr(c);
concreSubject->notify("messagebox 2");
//a->doSomeThing();
//b->doSomeThing();
//c->doSomeThing();
return 0;
}
//int main()
//{
// AObserver* a = new AObserver();
// BObserver* b = new BObserver();
// CObserver* c = new CObserver();
// ConcreteSubject* concreSubject = new ConcreteSubject();
//
// concreSubject->addObsvr(a);
// concreSubject->addObsvr(b);
// concreSubject->addObsvr(c);
// concreSubject->notify("messagebox 1");
//
// concreSubject->removeObsvr(c);
// concreSubject->notify("messagebox 2");
//
// //a->doSomeThing();
// //b->doSomeThing();
// //c->doSomeThing();
// return 0;
//}
/*****************************打印结果*******************************