Design Pattern Examples
Overview of object-oriented design patterns
Observer_Exercise.cpp
Go to the documentation of this file.
1
5
6#include <iostream>
7
8#include "helpers/formatstring.h"
9
10#include "Observer_Exercise.h"
11#include "Observer_Class.h"
12
13
15{
16
37 // ! [Using Observer in C++]
39 {
40 std::cout << std::endl;
41 std::cout << "Observer Exercise" << std::endl;
42
43 INumberProducer::shared_ptr_t numberProducer = std::make_shared<ObserverSubject_NumberProducer>();
44
45 // The number producer is passed to the observers so the observers
46 // can get the number to display. The observers only see the
47 // INumberProducer interface, to minimize knowledge about the
48 // Subject.
49 IObserverNumberChanged::shared_ptr_t observerDecimal = std::make_shared<ObserverForDecimal>(numberProducer);
50 IObserverNumberChanged::shared_ptr_t observerHexadecimal = std::make_shared<ObserverForHexaDecimal>(numberProducer);
51 IObserverNumberChanged::shared_ptr_t observerBinary = std::make_shared<ObserverForBinary>(numberProducer);
52
53 // Tell the number producer about the observers who are notified
54 // whenever the value changes.
55 IEventNotifications* eventNotifier = dynamic_cast<IEventNotifications*>(numberProducer.get());
56 if (eventNotifier != nullptr)
57 {
58 eventNotifier->SubscribeToNumberChanged(observerDecimal);
59 eventNotifier->SubscribeToNumberChanged(observerHexadecimal);
60 eventNotifier->SubscribeToNumberChanged(observerBinary);
61 }
62
63 // Call the number producer's Update() method a number of times.
64 // The observers automatically print out the current value in
65 // different bases.
66 for (int index = 0; index < 10; ++index)
67 {
68 std::cout
69 << Helpers::formatstring(" Update %d on number producer. Results from observers:", index)
70 << std::endl;
71 numberProducer->Update();
72 }
73
74 if (eventNotifier != nullptr)
75 {
76 // When done, remove the observers from the number producer.
77 // It's always good to clean up after ourselves.
78 eventNotifier->UnsubscribeFromNumberChanged(observerDecimal);
79 eventNotifier->UnsubscribeFromNumberChanged(observerHexadecimal);
80 eventNotifier->UnsubscribeFromNumberChanged(observerBinary);
81 }
82
83 std::cout << " Done." << std::endl;
84 }
85 // ! [Using Observer in C++]
86
87} // end namespace
Implementation for the ObserverForDecimal, ObserverForHexaDecimal, and ObserverForBinary classes used...
Declaration of the Observer_Exercise() function as used in the Observer Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Observer_Exercise()
Example of using the Observer design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Represents a Subject that takes observers implementing the IObserverNumberChanged interface.
virtual void UnsubscribeFromNumberChanged(IObserverNumberChanged::shared_ptr_t observer)=0
virtual void SubscribeToNumberChanged(IObserverNumberChanged::shared_ptr_t observer)=0
std::shared_ptr< INumberProducer > shared_ptr_t
Alias to make it easier to use this shared pointer.
std::shared_ptr< IObserverNumberChanged > shared_ptr_t
Alias to make it easier to use this shared pointer.