Add example of observer pattern in C++
This commit is contained in:
parent
1c78867d91
commit
53ee3df451
|
@ -22,3 +22,4 @@ add_subdirectory(factory)
|
|||
add_subdirectory(prototype)
|
||||
add_subdirectory(abstract-factory)
|
||||
add_subdirectory(state)
|
||||
add_subdirectory(observer)
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
###############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: A project for practicing the observer C++ design pattern ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
#
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(
|
||||
#[[NAME]] Observer
|
||||
VERSION 1.0
|
||||
DESCRIPTION "An example of the state design pattern in C++"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
add_compile_options("-Wall")
|
||||
|
||||
add_library(observer "observer.cpp")
|
||||
add_executable(observer-test "main.cpp")
|
||||
target_link_libraries(observer-test observer)
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "observer.hpp"
|
||||
|
||||
int main(const int argc, const char * argv[])
|
||||
{
|
||||
Subject subject("Radio");
|
||||
Observer *john = new Observer(subject);
|
||||
|
||||
// Test alerting john as the only subscribed observer
|
||||
std::cout << "\nAlerting one observer...\n\n";
|
||||
subject.SendMessage("Alert 1\n");
|
||||
|
||||
|
||||
// Attach a new observer
|
||||
Observer joe(subject);
|
||||
// Alert all subscribed observers
|
||||
std::cout << "\nAlerting all observers...\n\n";
|
||||
subject.SendMessage("Alert 2\n");
|
||||
|
||||
std::cout << std::endl;
|
||||
// Detach joe from the subject
|
||||
joe.Detach();
|
||||
// Send another alert to john
|
||||
std::cout << "Alerting one observer...\n\n";
|
||||
subject.SendMessage("Alert 3\n");
|
||||
john->Detach();
|
||||
|
||||
// This alert will not be heard by anyone
|
||||
std::cout << "\nAlerting no observers...\n\n";
|
||||
subject.SendMessage("Alert 4\n");
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
#include "observer.hpp"
|
||||
|
||||
void Subject::Notify()
|
||||
{
|
||||
for (const auto &o : observerList) { o->Update(message);}
|
||||
}
|
||||
|
||||
void Subject::SendMessage(std::string message_)
|
||||
{
|
||||
message = message_;
|
||||
Notify();
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Observer
|
||||
|
||||
int Observer::totalObservers = 0;
|
||||
|
||||
Observer::Observer(Subject &s) : subject(s) {
|
||||
std::cout << "Observer " << ++totalObservers << " is subscribed to "
|
||||
<< s.getName() << "\n";
|
||||
s.Attach(this);
|
||||
number = totalObservers;
|
||||
}
|
||||
|
||||
void Observer::Update(std::string &message)
|
||||
{
|
||||
std::cout << "Observer " << number << " has message from "
|
||||
<< subject.getName() << ": " << message;
|
||||
}
|
||||
|
||||
void Observer::Detach()
|
||||
{
|
||||
std::cout << "Observer " << number << " has detached from "
|
||||
<< subject.getName() << std::endl;
|
||||
subject.Detach(this);
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
|
||||
#ifndef OBSERVER_HPP
|
||||
#define OBSERVER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
|
||||
#include "observer.hpp"
|
||||
|
||||
class Observer;
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
Subject(std::string name_) : name(name_) {}
|
||||
virtual ~Subject() { std::cout << name << " subject destroyed\n";}
|
||||
|
||||
void Attach(Observer * o) { observerList.push_back(o);}
|
||||
void Detach(Observer * o) { observerList.remove(o);}
|
||||
void Notify();
|
||||
|
||||
void SendMessage(std::string message_);
|
||||
inline std::string getName() const { return name;}
|
||||
|
||||
private:
|
||||
std::list<Observer *> observerList;
|
||||
std::string message, name;
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
// Observer
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
Observer(Subject &s);
|
||||
|
||||
void Update(std::string &message);
|
||||
void Detach();
|
||||
|
||||
private:
|
||||
Subject &subject;
|
||||
int number;
|
||||
static int totalObservers;
|
||||
};
|
||||
|
||||
#endif // OBSERVER_HPP
|
Loading…
Reference in New Issue