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 :

It’s Jdecoder

I am trying to decode the concepts into simple words and documenting items i know or currently learning.

Let’s connect

Design a site like this with WordPress.com
Get started