Observer (Design pattern)

In this behavioral design pattern all observer getting notified of a change from a subject they are registered at.

#include <string>
#include <set>
#include <algorithm>
#include <iostream>

using namespace std;

// --- Observer ----------
class Observer
{
public:
    virtual void onInput(string) = 0;
};

// --- Subject ----------
class Subject
{
public:
    virtual void attach(Observer*) = 0;
    virtual void detach(Observer*) = 0;
    virtual void input(string) = 0;
};

// --- Implementation ---
class View : Subject
{
private:
    set<Observer*> obs;
public:
    void attach(Observer *ob){obs.insert(ob);}
    void detach(Observer *ob){obs.erase(ob);}
    void input(string input)
    {
        for(auto ob : obs){
            ob->onInput(input);
        }
    }
};

class Parser : Observer
{
public:
    void onInput(string input)
    {
        parse(input);
    }
private:
    void parse(string input)
    {
        cout << "Parse: " + input << endl;
    }
};


int main()
{
    View view;
    Parser parser;
    view.attach((Observer*)&parser);
    
    // --- Emulate Input ---
    view.input("(3+2)*4");

    return 0;
}