forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverMain.cpp
More file actions
34 lines (26 loc) · 769 Bytes
/
ObserverMain.cpp
File metadata and controls
34 lines (26 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// Created by xiemenghui on 2018/7/21.
//
#include "ObserverMain.h"
void ObserverMain()
{
// Create Subject
ConcreteSubject * pSubject = new ConcreteSubject();
// Create Observer
IObserver * pObserver1 = new ConcreteObserver("Jack Ma");
IObserver * pObserver2 = new ConcreteObserver("Pony");
// Attach Observers
pSubject->Attach(pObserver1);
pSubject->Attach(pObserver2);
// Change the price and notify the observer
pSubject->SetPrice(12.5);
pSubject->Notify();
// Detach Observers
pSubject->Detach(pObserver2);
// Change the state again and notify the observer
pSubject->SetPrice(15.0);
pSubject->Notify();
SAFE_DELETE(pObserver1);
SAFE_DELETE(pObserver2);
SAFE_DELETE(pSubject);
}