Implementing Mediator Pattern in C++

From: cppaddict (
Date: 08/05/04

Date: Thu, 05 Aug 2004 06:40:45 GMT

I have a design question which I am posting here because the
implementation will be in C++ and I think there may be C++ specific
language constructs (eg, friends) that might be relevant to the

I am implementing the Mediator pattern to create an event-driven app.
The Collaborators (which watch for various events and which all have a
reference to the Mediator object) will call handleEvent(Event e) on
the Mediator object when they observe a relevant event.

Right now, my Mediator object is branching on the event to do the

if (e.type == "Event1")
        //tell collaborator 2 to do some stuff
        //tell collaborator 3 to do some stuff
else if (e.type == "Event2")
        //handle event2

But I can't stand the look of this design. It is, for one, closed to
the addition of new event handlers (ie, I have to modify the branching
code in Mediator if I want to handle a new event or modify the
handling of an event).

It seems there should be a way to add EventHandler objects (with
member variables specifying what types of events they handle) to the
Mediator. When the Mediator received an event, it could look through
its handlers and execute a handleEvent() method on each relevant
handler. The problem is that the EventHandler objects don't have
access rights to call methods on the Collaborators, which are private
members of Mediator.

How should I handle this problem? Pass those members into the
EventHandlers when they are created, perhaps in Mediator's ctor? Or I
could declare the EventHandlers as friends? But I would probably need
to put the EventHandlers into a vector -- can you declare a vector of

Or is there some more fundamental flaw to this design??

Thanks for any help,