#include <iostream>
#include <list>
using namespace std;
class Iobserver
{
public:
virtual void notify(string msg) = 0;
};
class observer1 : public Iobserver
{
public:
void notify(string msg)
{
cout<<"Observer1 : "<<msg<<endl;
}
};
class observer2 : public Iobserver
{
public:
void notify(string msg)
{
cout<<"Observer2 : "<<msg<<endl;
}
};
class observer3 : public Iobserver
{
public:
void notify(string msg)
{
cout<<"Observer3 : "<<msg<<endl;
}
};
class observermanager
{
list<Iobserver*>users;
public:
void register_user(Iobserver* usr)
{
users.push_back(usr);
}
void deregister_user(Iobserver* usr)
{
users.remove(usr);
}
void send_notification(string msg)
{
for(auto *itr:users)
{
itr->notify(msg);
}
}
};
int main() {
// observer1 user1;
// observer2 user2;
// observer3 user3;
Iobserver *user1 = new observer1();
Iobserver *user2 = new observer2();
Iobserver *user3 = new observer3();
observermanager *obsmgr = new observermanager();
obsmgr->register_user(user1);
obsmgr->register_user(user2);
obsmgr->register_user(user3);
obsmgr->send_notification("this is a messsage");
obsmgr->deregister_user(user2);
obsmgr->send_notification("new messsage");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: