Mediator pattern can be used when colleague need to talk to each other, without talking to each other.c1 (colleague 1 ) talks to Mediator -> If needed Mediator communicates to c2.

class Notification {
int mId;
string mName;
public:
Notification(int id, string name) {
mId = id;
mName = name;
}
int getId() { return mId;}
string getName() { return mName;}
};
// Mediator Interface
class INotificationManager {
public:
virtual ~INotificationManager() {}
virtual void add(int id, string name) = 0;
virtual void remove(int id) = 0;
virtual void update (int id, string name) = 0;
virtual int count() = 0;
};
// Mediator Concrete Impl
class NotificationManager : public INotificationManager {
unordered_map<int, shared_ptr<Notification>> mNotificationMap;
public:
void add(int id, string name) {
cout <<" notification add " << id << " name " << name <<endl;
mNotificationMap[id] = make_shared<Notification>(id, name);
}
void remove(int id) {
if (mNotificationMap.count(id)) {
cout <<" notification removed " << id <<endl;
mNotificationMap.erase(id);
}
}
void update (int id, string name) {
mNotificationMap[id] = make_shared<Notification>(id, name);
cout <<" notification updated for " << id << " name " << name <<endl;
}
int count () {
return mNotificationMap.size();
}
};
class IClient {
public:
virtual ~IClient () {};
virtual void notify(int id, string name) = 0;
};
class Acitivity : public IClient {
shared_ptr<INotificationManager> nm;
public :
Acitivity(shared_ptr<INotificationManager> & nm) {
this->nm = nm;
}
void notify(int id, string name) {
//cout <<" activity notifiying ...." <<endl;
nm->add(id, name);
}
};
class Service : public IClient {
shared_ptr<INotificationManager> nm;
public :
Service(shared_ptr<INotificationManager> & nm) {
this->nm = nm;
}
void notify(int id, string name) {
//cout <<" Service notifiying ...." <<endl;
nm->update(id, name);
}
};
class BroadcastRec : public IClient {
shared_ptr<INotificationManager> nm;
public :
BroadcastRec(shared_ptr<INotificationManager> & nm) {
this->nm = nm;
}
void notify(int id, string name) {
//cout <<" BroadcastRec notifiying ...." <<endl;
nm->remove(id);
}
};
int main() {
shared_ptr<INotificationManager> nm = make_shared<NotificationManager>();
Acitivity a(nm);
Service s(nm);
BroadcastRec b(nm);
a.notify(1, "notification from activity");
s.notify(2, "notification from service");
b.notify(1, "notification from broadcast");
cout <<" notification count " << nm->count() <<endl;
}
Follow more posts @ https://jdecodes.wordpress.com
My all design pattern articles :
- https://jdecodes.wordpress.com/2024/07/13/builder-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/command-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/iterator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/mediator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/state-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/memento-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/observer-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/strategy-pattern/
- https://jdecodes.wordpress.com/2024/07/20/visitor-design-pattern/
- https://jdecodes.wordpress.com/2024/07/20/adapter-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/20/bridge-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/composite-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/22/facade-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/decorater-design-pattern/
Leave a comment