mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
34 lines
712 B
C++
34 lines
712 B
C++
|
//
|
|||
|
// Created by xiemenghui on 2018/7/21.
|
|||
|
//
|
|||
|
|
|||
|
#include "ObserverMain.h"
|
|||
|
|
|||
|
void ObserverMain()
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
ConcreteSubject * pSubject = new ConcreteSubject();
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>۲<EFBFBD><DBB2><EFBFBD>
|
|||
|
IObserver * pObserver1 = new ConcreteObserver("Jack Ma");
|
|||
|
IObserver * pObserver2 = new ConcreteObserver("Pony");
|
|||
|
|
|||
|
// ע<><D7A2><EFBFBD>۲<EFBFBD><DBB2><EFBFBD>
|
|||
|
pSubject->Attach(pObserver1);
|
|||
|
pSubject->Attach(pObserver2);
|
|||
|
|
|||
|
// <20><><EFBFBD>ļ۸<DBB8>֪ͨ<CDA8>۲<EFBFBD><DBB2><EFBFBD>
|
|||
|
pSubject->SetPrice(12.5);
|
|||
|
pSubject->Notify();
|
|||
|
|
|||
|
// ע<><D7A2>һ<EFBFBD><D2BB><EFBFBD>۲<EFBFBD><DBB2><EFBFBD>
|
|||
|
pSubject->Detach(pObserver2);
|
|||
|
|
|||
|
// <20>ٴθ<D9B4><CEB8><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>֪ͨ<CDA8>۲<EFBFBD><DBB2><EFBFBD>
|
|||
|
pSubject->SetPrice(15.0);
|
|||
|
pSubject->Notify();
|
|||
|
|
|||
|
SAFE_DELETE(pObserver1);
|
|||
|
SAFE_DELETE(pObserver2);
|
|||
|
SAFE_DELETE(pSubject);
|
|||
|
}
|