-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use observer pattern in view interface, code refactor
- Loading branch information
Showing
9 changed files
with
85 additions
and
42 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* observer.hpp | ||
* | ||
* Created on: 13 wrz 2023 | ||
* Author: kwarc | ||
*/ | ||
|
||
#ifndef OBSERVER_HPP_ | ||
#define OBSERVER_HPP_ | ||
|
||
#include <vector> | ||
|
||
namespace middlewares | ||
{ | ||
|
||
template<typename T> | ||
class observer | ||
{ | ||
public: | ||
virtual ~observer() {}; | ||
virtual void update(const T &data) = 0; | ||
}; | ||
|
||
template<typename T> | ||
class subject | ||
{ | ||
public: | ||
virtual ~subject() {}; | ||
|
||
void attach(observer<T> *o) | ||
{ | ||
this->observers.push_back(o); | ||
} | ||
|
||
void detach(observer<T> *o) | ||
{ | ||
this->observers.erase(std::remove(this->observers.begin(), this->observers.end(), o), this->observers.end()); | ||
} | ||
|
||
void notify(const T &data) | ||
{ | ||
for (const auto &o : this->observers) | ||
o->update(data); | ||
} | ||
private: | ||
std::vector<observer<T>*> observers; | ||
}; | ||
|
||
} | ||
|
||
#endif /* OBSERVER_HPP_ */ |