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. * 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 ABC * Subject Observer ABC
* *
* 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;
} //}
/*****************************打印结果******************************* /*****************************打印结果*******************************