samedi 5 septembre 2015

Observer with multiple subjects

I have seen implementations of the Observer design pattern in which the Observer is responsible for multiple Subjects. Most of these implementations use a std::vector<Subject*> in order to keep track of the Subjects.

Would it be possible for me to do a similar thing, using a std::unordered_set<weak_ptr<Subject>> instead?

The reason I want to use an unordered_set is that I will not need duplicates, and I don't need an ordered container. From what I understand, an unordered_set is the way to go in this situation.

If you disagree, leave an answer explaining what container I should use instead. If i did use the unordered_set, I would have to declare a hash function for the weak_ptr, but could this be accomplished by just using the hash function for the pointer inside, obtained with subjects.lock().get()?

Aucun commentaire:

Enregistrer un commentaire