Add example of observer pattern in C++

This commit is contained in:
Shaun Reed 2021-05-11 19:58:23 -04:00
parent 1c78867d91
commit 53ee3df451
5 changed files with 140 additions and 0 deletions

View File

@ -22,3 +22,4 @@ add_subdirectory(factory)
add_subdirectory(prototype)
add_subdirectory(abstract-factory)
add_subdirectory(state)
add_subdirectory(observer)

View File

@ -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)

View File

@ -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");
}

View File

@ -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);
}

View File

@ -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