In observer pattern, subject notices a change to observer. But I also need subject to be notified by observer.
I went something like this for the purpose:
//Observer.h
class Subject;
class Observer{
public:
void update(Subject* p_subject);
void Subject_Notify();
private:
Subject* subject;
}
//Subject.h
#include "Observer.h"
class Subject{
public:
void observer_notify()
{
observer->update(this);
}
void update(Observer* observer);
private:
Observer* observer;
}
//Observer.cpp
#include "Observer.h"
#include "Subject.h"
void Observer::update(Subject* p_subject)
{
//do something with p_subject
}
void Observer::Subject_Notify()
{
subject->update(this);
}
The code seems complicated and I am not sure if I am going in the right direction. Is there alternative or is this just ok?