I am working with an external library and need to create an observer pattern where observers are derived from an object that belongs to the library. I do not want to change the base class from the library and at the same time I have to use the list of references/pointers to this unchangeable base class. On top of this the library constructs the list of objects from which I need to sift the ones that are fit to be observers.
The code I wrote is roughly equivalent to this:
#include <iostream>
#include <vector>
#include <memory>
// This class is from an external library which I don't want to chagne
class BaseFromLibrary {
public:
virtual ~BaseFromLibrary() {}
};
class BaseOfObserver {
public:
void notify() { std::cout << "What-ho!\n"; };
};
class Observer : public BaseFromLibrary, public BaseOfObserver {};
class Subject {
public:
std::vector<std::shared_ptr<Observer>> observers;
void notifyObervers() {
for (auto &o : observers)
(*o).notify();
}
};
int main() {
// This list is constructed by the library and I cannot interfere with that
// process
std::vector<std::shared_ptr<BaseFromLibrary>> list{
std::make_shared<BaseFromLibrary>(), std::make_shared<Observer>()};
Subject s;
for (auto &e : list)
if (std::dynamic_pointer_cast<Observer>(e) != nullptr)
s.observers.push_back(std::dynamic_pointer_cast<Observer>(e));
s.notifyObervers();
}
Then I use the BaseOfObserver
to add "subject awareness" to my other derived types. This way I do not have to repeat an if-statement for each specific observer I want to implement.
It seems to work fine, but is that a design mistake? Is there a better way to construct the observer list without RTTI mechanism and without interfering with the library classes?
list
from my example. I may have forgotten to emphasize that this is also something I cannot do. The library constructs that list on its own and I do not want to interfere with that process. I'll try to edit that. Point taken about memory leak. – Sharonsharona