Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Last active April 6, 2021 00:27
Show Gist options
  • Save Journeyman1337/a7589edd396df6c1ad071e6246249201 to your computer and use it in GitHub Desktop.
Save Journeyman1337/a7589edd396df6c1ad071e6246249201 to your computer and use it in GitHub Desktop.
observer/listener pattern with template version that publishes the data in an argument to all observers (subscribers).
/*
Copyright 2021 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <observer.h>
void Subject::RegisterObserver(IObserver* observer)
{
this->observers.push_back(observer);
}
void Subject::RemoveObserver(IObserver* observer)
{
this->observers.erase(std::remove(this->observers..begin(), this->observers..end(), observer), this->observers..end());
}
void Subject::Notify()
{
for (const auto& o : this->observers)
{
o->Update();
}
}
IObserver::IObserver(const Subject* subject)
{
this->subject = subject;
this->subject->RegisterObserver(this);
}
IObserver::~IObserver()
{
if (this->subject)
this->subject->RemoveObserver(this);
}
void IObserver::NotifySubjectDestroyed()
{
this->subject = nullptr;
}
/*
Copyright 2021 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <vector>
class Subject;
class Observer;
//Standard observer pattern subject.
class Subject
{
private:
std::vector<Observer*> observers;
public:
Subject() = default;
~Subject() = default;
void RegisterObserver(Observer* observer);
void RemoveObserver(Observer* observer);
void Notify();
};
//Standard observer pattern observer.
class Observer
{
private:
Subject* subject;
public:
Observer(Subject* subject);
~Observer();
virtual void Update() = 0;
void NotifySubjectDestroyed();
};
template<typename T>
class Subscriber;
template<typename T>
class Publisher;
//Data passing observer pattern subject.
template<typename T>
class Publisher
{
private:
std::vector<Subscriber<T>*> subscribers;
public:
Publisher() : subscribers() {}
~Publisher()
{
for (const auto& s : this->subscribers)
{
s->NotifyPublisherDestroyed();
}
}
void RegisterSubscriber(Subscriber<T>* subscriber)
{
this->subscribers.push_back(subscriber);
}
void RemoveSubscriber(Subscriber<T>* subscriber)
{
this->subscribers.erase(std::remove(this->subscribers.begin(), this->subscribers.end(), subscriber), this->subscribers.end());
}
void Publish(const T& data)
{
for (const auto& s : this->subscribers)
{
s->Recieve(data);
}
}
};
//Data recieving observer pattern observer.
template<typename T>
class Subscriber
{
private:
Publisher<T>* publisher;
public:
Subscriber(Publisher<T>* publisher)
{
this->publisher = publisher;
this->publisher->RegisterSubscriber(this);
}
~Subscriber()
{
if (this->publisher)
this->publisher->RemoveSubscriber(this);
}
virtual void Recieve(const T& data) = 0;
void NotifyPublisherDestroyed()
{
this->publisher = nullptr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment