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
| #include <iostream> #include "Subject.h" #include "Observer.h" #include "ConcreteObserver.h" #include "ConcreteSubject.h"
int main(int argc, char *argv[]) { Subject* subjectA = new ConcreteSubject("subjectA"); Subject* subjectB = new ConcreteSubject("subjectB"); Observer* observerA = new ConcreteObserver("observerA", subjectA); Observer* observerB = new ConcreteObserver("observerB", subjectB); subjectA->addObserver(observerA); subjectB->addObserver(observerB); subjectA->setStatus(1); subjectA->notifyObservers(); subjectB->setStatus(2); subjectB->notifyObservers(); subjectA->addObserver(observerB); subjectA->setStatus(2); subjectA->notifyObservers(); delete subjectA; delete subjectB; delete observerA; delete observerB; return 0; }
|