Design Pattern Examples
Overview of object-oriented design patterns
Observer_Class.h
Go to the documentation of this file.
1
7
8#pragma once
9#ifndef __OBSERVER_CLASS_H__
10#define __OBSERVER_CLASS_H__
11
12#include <iostream>
13#include <string>
14
16#include "helpers/formatstring.h"
17#include "helpers/uint32_to_binary.h"
18
20
22{
23
29 {
30 private:
35
36 public:
43 : _numberProducer(numberProducer)
44 {
45 if (!numberProducer)
46 {
47 throw Helpers::argumentnull_error("numberProducer",
48 "The ObserverForDecimal constructor requires a valid INumberProducer object.");
49 }
50 }
51
62 {
63 uint32_t number = _numberProducer->FetchNumber();
64 std::cout << Helpers::formatstring(" Decimal : %u", number) << std::endl;
65 }
66 };
67
68
69 //########################################################################
70 //########################################################################
71
72
78 {
79 private:
84
85 public:
92 : _numberProducer(numberProducer)
93 {
94 if (!numberProducer)
95 {
96 throw Helpers::argumentnull_error("numberProducer", "The ObserverForHexaDecimal constructor requires a valid INumberProducer object.");
97 }
98 }
99
100
112 {
113 uint32_t number = _numberProducer->FetchNumber();
114 std::cout << Helpers::formatstring(" Hexadecimal: %#08X", number) << std::endl;
115 }
116 };
117
118
119 //########################################################################
120 //########################################################################
121
122
128 {
129 private:
134
135 public:
142 : _numberProducer(numberProducer)
143 {
144 if (!numberProducer)
145 {
146 throw Helpers::argumentnull_error("numberProducer", "The ObserverForBinary constructor requires a valid INumberProducer object.");
147 }
148 }
149
150
161 {
162 uint32_t number = _numberProducer->FetchNumber();
163 std::string binary = Helpers::uint32_to_binary(number);
164 std::cout << Helpers::formatstring(" Binary : %s", binary.c_str())
165 << std::endl;
166 }
167 };
168
169} // end namespace
170
171#endif // __OBSERVER_CLASS_H__
172
The IObserverNumberChanged, IEventNotifications, and INumberProducer interfaces, and the ObserverSubj...
Implementation of the argumentnull_error exception.
Represents an observer that prints out the current number from the Subject in binary.
ObserverForBinary(INumberProducer::shared_ptr_t numberProducer)
Constructor.
INumberProducer::shared_ptr_t _numberProducer
The number producer from which to get the current number.
void NumberChanged()
Called whenever the number is changed in the number producer. This observer instance must first be su...
Represents an observer that prints out the current number from the Subject in decimal.
INumberProducer::shared_ptr_t _numberProducer
The number producer from which to get the current number.
void NumberChanged()
Called whenever the number is changed in the number producer. This observer instance must first be su...
ObserverForDecimal(INumberProducer::shared_ptr_t numberProducer)
Constructor.
Represents an observer that prints out the current number from the Subject in hexadecimal.
INumberProducer::shared_ptr_t _numberProducer
The number producer from which to get the current number.
ObserverForHexaDecimal(INumberProducer::shared_ptr_t numberProducer)
Constructor.
void NumberChanged()
Called whenever the number is changed in the number producer. This observer instance must first be su...
Exception for arguments that are null.
The namespace containing all Design Pattern Examples implemented in C++.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::string uint32_to_binary(uint32_t number)
Function to convert a 32-bit unsigned integer into a string representation containing all 32 bits.
std::shared_ptr< INumberProducer > shared_ptr_t
Alias to make it easier to use this shared pointer.
Represents an observer to the Subject class. An observer implements this interface and then subscribe...