I'm reading on design patterns for a software engineering class. I am doing small implementations of some I find the most interesting / applicable to better understand them.
I'd like a code review on my design, more particularly on the degree of coupling between my Observer and Publisher classes. I feel that the add_observer()
function may be coupled too strongly with the Observer
object and that there may be a way to do this with looser coupling.
Observer.h
#ifndef OBSERVER_H_INCLUDED
#define OBSERVER_H_INCLUDED
#include <string>
class Publisher;
class Observer {
public:
virtual void eventOnProperty(Publisher* source,
std::string event_name, std::string value) = 0;
};
#endif
Publisher.h
#ifndef PUBLISHER_H_INCLUDED
#define PUBLISHER_H_INCLUDED
#include "Observer.h"
#include <list>
#include <string>
class Publisher {
public:
void notify_observers(std::string, std::string);
void add_observer(Observer*);
protected:
std::list<Observer*> my_observers;
};
#endif
Publisher.cpp
#include "Publisher.h"
#include "Observer.h"
#include <string>
#include <list>
void Publisher::notify_observers(std::string event_name, std::string value) {
std::list<Observer*>::iterator it;
for(it = my_observers.begin(); it != my_observers.end(); it++) {
(*it)->eventOnProperty(this, event_name, value);
}
}
void Publisher::add_observer(Observer* obs) {
my_observers.push_back(obs);
}
virtual
destructors. Also, ownership semantics are somewhat unclear here. \$\endgroup\$ – Yuushi May 26 '13 at 4:28