-
-
Save alersenkevich/e0cd3f576630f54c9ce83f9f3ad345e1 to your computer and use it in GitHub Desktop.
oserver realization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <list> | |
using namespace std; | |
class SupervisedString; | |
class IObserver | |
{ | |
public: | |
virtual void handleEvent(const SupervisedString&) = 0; | |
}; | |
class SupervisedString // Observable class | |
{ | |
string _str; | |
list<IObserver*> _observers; | |
void _Notify() | |
{ | |
for(list<IObserver*>::iterator iter = _observers.begin(); iter != _observers.end(); ++iter) | |
{ | |
(*iter)->handleEvent(*this); | |
} | |
} | |
public: | |
void add(IObserver& ref) | |
{ | |
_observers.push_back(&ref); | |
} | |
void remove(IObserver& ref) | |
{ | |
_observers.remove(&ref); | |
} | |
const string& get() const | |
{ | |
return _str; | |
} | |
void reset(string str) | |
{ | |
_str = str; | |
_Notify(); | |
} | |
}; | |
class Reflector: public IObserver // Prints the observed string into cout | |
{ | |
public: | |
virtual void handleEvent(const SupervisedString& ref) | |
{ | |
cout « ref.get() « endl; | |
} | |
}; | |
class Counter: public IObserver // Prints the length of observed string into cout | |
{ | |
public: | |
virtual void handleEvent(const SupervisedString& ref) | |
{ | |
cout « "length = " « ref.get().length() « endl; | |
} | |
}; | |
int main() | |
{ | |
SupervisedString str; | |
Reflector refl; | |
Counter cnt; | |
str.add(refl); | |
str.reset("Hello, World!"); | |
cout « endl; | |
str.remove(refl); | |
str.add(cnt); | |
str.reset("World, Hello!"); | |
cout « endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment