cpp-interview/DesignPattern/ObserverPattern/ObserverMain.cpp

34 lines
712 B
C++
Raw Normal View History

//
// 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);
}