473,703 Members | 3,315 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing Mediator Pattern in C++

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
solution.

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(Eve nt e) on
the Mediator object when they observe a relevant event.

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

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
etc.....

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
friends???

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

Thanks for any help,
cpp
Jul 22 '05 #1
2 3595
"cppaddict" <he***@hello.co m> wrote in message
news:te******** *************** *********@4ax.c om...
hi,
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(Eve nt e) on
the Mediator object when they observe a relevant event.

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

if (e.type == "Event1") Is such a switch-like statement necessary?
In implementations of the Mediator pattern, it is common to have
one method/handler for each type of event.
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). Ok, let's assume that the possible events are not known a priori.
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
friends???

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

Because you do not describe the problem to be solved (but only
a proposed solution), we have to guess what you are trying to do.
But it seems to me that you only want to pass on the events to the
Collaborators, and that your Mediator does not have much
intelligence.
So maybe you should consider an Observer or Broadcaster-Listener
design pattern.
For example:

class Event { /*...*/ };

class IListener { // interface for all listeners
public:
void listen(Event& event);

virtual ~IListener() {} // or make it protected?
};

class Broadcaster
{
private:
// ... manage a list of listeners, maybe using an std::vector
// or an std::set<IListe ner*>
bool include(Listene r& listener); // --> true not already present
bool exclude(Listene r& listener); // --> true if removed (was included)

void broadcast(Event & event)
{
... call listen(event) on each connected Listener ...
}
};
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
cppaddict <he***@hello.co m> wrote:
I am implementing the Mediator pattern to create an event-driven app.
You say you are implementing a Mediator pattern, but what you describe
doesn't sound much like one...

The Collaborators (which watch for various events and which all have a
reference to the Mediator object) will call handleEvent(Eve nt e) on
the Mediator object when they observe a relevant event.
By "Collaborat ors" I assume you mean the classes the book calls
Colleagues?

Do the Collaborators create the event objects, or are the event objects
created elsewere and sent to the Collaborators via a Chain of
Responsibility?

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

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
etc.....

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).
Does each Collaborator listen for multiple events?

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.


The Mediator could pass the Collaborators to the EventHandler in
question.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1827
by: Coder-X | last post by:
Hi, can someone give a real app example of where the Mediator design pattern could be used ( for instance in a relational database application )? Thanks, José Carlos Ferreira
0
987
by: Horst Klein | last post by:
Hi I'm searching a sample how to use a ChangeManager (Based on the Mediator Pattern) How can help me? Best regards Horst
1
2709
by: FluffyCat | last post by:
I finally pieced together what I think is a good example of the Mediator Pattern in PHP 5. See what you think. http://www.fluffycat.com/PHP-Design-Patterns/Mediator/ I have, per request, tried to simplify my test program structure a bit. testMediator.php has only one html tag in it - a <BR>. This is in my new writeln() function, which adds '<'.'BR'.'>' to each line and
4
3328
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
2
3393
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and I'd like the attention of fp as well as C++ gurus if available. The language I'm implementing - PILS - is dynamically
1
1729
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that had no connection or reference to the object that triggered it. It goes something like this: (not syntactically correct..it's just for the idea)
1
4159
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that had no connection or reference to the object that triggered it. It goes something like this: (not syntactically correct..it's just for the idea)
4
2112
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
9
2398
by: raylopez99 | last post by:
Here are two different ways of achieving a mediator pattern: the first, using circular references (for lack of a better way to describe it), but not using delegates, with the second using delegates. The first way is an adaptation from C++ for Dummies by Jeff Cogswell. But obviously it uses references not pointers. The second way (I'll post this later, as I haven't yet done it) will be adapted from the book C#3.0 Design Patterns by...
0
9246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9112
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7856
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4680
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2440
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.