473,756 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c#: inter-object event messaging (mediator pattern?)

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)

main()
{ A a;
C c;
a.Run();
}

class A
{ B b;
C c;

Run()
{ // Trigger the Event here}
} // class

class B
{ C c;
}

class C
{ // Catch and handle the event here
}

Which means that the event would be handled by the 3 instances of C.

>From My research I gathered that I should be using something called
the "Mediator Pattern".
Unfortunately I could not find a working example of this.

Also, from what I was able to understand, to make it work with this
pattern would require that I derive class C from an abstract
"participan t". Is that right ?

I then kind of gave up on trying to make it work with this pattern
(out of frustration at my failures) and proceeded to try and implement
something that I was a bit more familiar with (from my backgroung with
PCS):
The Publish / Subscribe model (is that also known as a pattern?)
I was pleasantly surprised that it worked.
Here's my working implementation:

All classes use the following:
using System;
using System.Collecti ons.Generic;
using System.Text;

With the class Dispatcher also using:
using System.Collecti ons;
class Program
{
static void Main(string[] args)
{
A a = new A();
C c = new C("C1");

for(int i = 1; i <= 3; i++)
{
a.Run(i);
}
Console.ReadKey ();
}
}

class A
{
B b = new B();
C c = new C("C2");

public void Run(int i)
{
Dispatcher.Publ ish("MyEvent", "Hello #" + i.ToString());
}
}

class B
{
C c = new C("C3");
}

class C
{
private string name;

public C(string name)
{
this.name = name;
Dispatcher.Subs cribe("MyEvent" , OnMyEvent);
}

public void OnMyEvent(objec t appEventParams)
{
string textReceived = (string)appEven tParams;
Console.WriteLi ne(textReceived + " received in " + name);
}

}

public delegate void AppEventHandler (object appEventParams) ;

static class Dispatcher
{
private static Hashtable registry = new Hashtable();

public static void Subscribe(strin g appEvent, AppEventHandler
appEventHandler )
{
Subscription(ap pEvent, appEventHandler , true);
}

public static void Unsubscribe(str ing appEvent,
AppEventHandler appEventHandler )
{
Subscription(ap pEvent, appEventHandler , false);
}

private static void Subscription(st ring appEvent,
AppEventHandler appEventHandler , bool add)
{
ArrayList list;

if (add)
{
if (registry[appEvent] == null)
registry.Add(ap pEvent, new ArrayList());

list = (ArrayList)regi stry[appEvent];
list.Add(appEve ntHandler);
}
else
{
if (registry[appEvent] != null)
{
list = (ArrayList)regi stry[appEvent];
list.Remove(app EventHandler);
}
}
}

public static void Publish(string appEvent, object
appEventParams)
{
ArrayList list;
AppEventHandler appEventHandler ;

if (registry[appEvent] != null)
{
list = (ArrayList)regi stry[appEvent];

for (int i = 0; i < list.Count; i++)
{
appEventHandler = (AppEventHandle r)list[i];
if (appEventHandle r != null)
appEventHandler (appEventParams );
}
}
}
}
The ouput is:
Hello #1 received in C3
Hello #1 received in C2
Hello #1 received in C1
Hello #2 received in C3
Hello #2 received in C2
Hello #2 received in C1
Hello #3 received in C3
Hello #3 received in C2
Hello #3 received in C1

Here are my questions (about time you'll say...sorry about that too
long preambule):

1) Is 'this" somehow a valid implementation of the Mediator pattern
and if not, it is known under another name ? (I hope it's not one of
those anti-pattern)

2) If it's not a valid implementation of the Mediator pattern (or
another "good" pattern), is there any reason why you would advise
"against" this approach. Did I miss an obvious flaw ? Did I do a big
no-no ?

3) Could you "please" (pretty, pretty please) give me a valid
implementation of the Mediator pattern that would give me the exact
same behavior as in my example. (If possible please give me working
code)

4) Any other comments, good of bad, that you think might help me with
this problem.

Thanks a lot in advance.

Apr 25 '07 #1
1 4160
<ha*****@yahoo. comwrote in message
news:11******** *************@b 40g2000prd.goog legroups.com...
>
[...]
Which means that the event would be handled by the 3 instances of C.

>>From My research I gathered that I should be using something called
the "Mediator Pattern".
Unfortunately I could not find a working example of this.
I'd never heard the phrase "mediator pattern" until your post. :) As near
as I can tell from the Wikipedia article on the topic, the "mediator
pattern" is simply a way of describing an inter-object communications
paradigm in which an intermediate (or "mediating" ) object handles the
communications.

It seems to me that if you have a general-purpose event to which a variety
of different classes may want to subscribe, then the mediator pattern makes
sense. If, on the other hand, you have a special-purpose event to which
only a specific class will subscribe, I don't think you need a mediator
class and the mediator pattern doesn't make sense.
Also, from what I was able to understand, to make it work with this
pattern would require that I derive class C from an abstract
"participan t". Is that right ?
I guess that depends on how rigidly the authors of the "Design Patterns"
book have defined "mediator pattern".
I then kind of gave up on trying to make it work with this pattern
(out of frustration at my failures) and proceeded to try and implement
something that I was a bit more familiar with (from my backgroung with
PCS):
The Publish / Subscribe model (is that also known as a pattern?)
It seems to me that the publish/subscribe model you implemented is in fact
an example of a mediator pattern. Again, I suppose it really depends on how
rigidly the term has actually been defined. But in the code you wrote, an
intermediate class handles communications between other classes. As such,
that intermediate class could be thought of as a mediator, and the design
could be thought of as a "mediator pattern".
[...]
1) Is 'this" somehow a valid implementation of the Mediator pattern
and if not, it is known under another name ? (I hope it's not one of
those anti-pattern)
See above. I would call it an example of "the mediator pattern".
2) If it's not a valid implementation of the Mediator pattern (or
another "good" pattern), is there any reason why you would advise
"against" this approach. Did I miss an obvious flaw ? Did I do a big
no-no ?
I don't see anything obvious wrong with your implementation. It seems to me
that it could be a little more concise, especially if you don't really have
the need for named events (that is, if you really only have a single event
to deal with), since C# includes built-in support for a data type called an
"event" that wraps up the subscribe/unsubscribe functionality for you.
Likewise, if you know it will always be an instance of class C that
subscribes to the event, you could even have an explicit event within class
C to which instances add themselves and which is signaled by a specific
method in class C.

But all those are just variations on the theme, and if you really need the
general-purpose behavior you've implemented, then I don't really see much in
the way of significant improvements (I don't like the design of combining
both the subscribe and unsubscribe behavior into a single method, but that's
your choice I guess).

One minor quibble (besides the "two behaviors in one method I mention
above):

You might consider removing your ArrayList instance from the Hashtable
if you've just removed the last entry of the ArrayList. Not a big deal if
you have very few events, or if you have a lot of events but don't expect
any of them to be empty for very long (as in, clients only unsubscribe when
the application is shutting down). But it just seems nicer to me.

Now that you ask the question, it seems to me that the C# "event" is missing
something. That is, as near as I can tell you can only ever define a
specific instance of an event. You can't have the type of an event, which
means that you can't add a dynamically created event to a collection, which
means that you can't enjoy the type-safeness of events while at the same
time providing a named-event paradigm such as the one your Dispatcher class
does.

Maybe I'm wrong about the limitations of the C# "event". I'm no expert in
using them, and I might have missed something. If so, hopefully someone
else will pipe up. I think it would be really cool if you could use a
Dictionary to maintain a list of dynamically created events, indexed by
name. It wouldn't change the code you've suggested very much, but it would
allow it to be type-safe (that is, you would only be able to add delegates
of the correct type to a specific event).

Pete

Apr 25 '07 #2

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

Similar topics

4
1207
by: gmtonyhoyt | last post by:
Okay here's the situation. What I'm looking for, is a sort of generic wrapper or API that will allow me to build threads with some kind of messaging system attached with it. The idea is that these individual threads will each handle small messages to each other, where each thread managed to perform a seperate operation, yet their may be some inter-dependance. This is a Network based application, it recieves messages from client...
5
4132
by: Joseph Geretz | last post by:
I need to communicate between two applications. The legacy application is in VB6. New development is in C#. Here's the scenario: The VB6 app will be pumping document files into a folder. We'll be writing a new (C#) class for this new feature. The C# Application (as distinct from the file pumper class just mentioned) needs notification for each file pumped into the folder in order to process the file.
1
1603
by: Raf256 | last post by:
I have base template class A<B>, and son class B. Inside A<B> constrcutor, can I access a pointer to B, from "this"? like A<B>::A() : pointerToB(static_cast<B*>this) { } I will use the pointer.
1
1735
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)
11
1355
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C# forum because this is where most of the OO gurus hang out, and I view this as a fundamental issue of OO design. In ASP.NET, objects of type WebForm and UserControl have an intrinsic Page property which refers to their containing Page.
4
2241
by: Mark | last post by:
Hi, I'm relatively new to Java, but have been a programmer for decades. I would like multiple instances of my Java app (on different computers on the same local LAN) to communicate with each other through notifications of some sort. For example, when instance 1 does a certain action, instances 2, 3, and 4 should be notified about it so they can react accordingly. I'm unaware of existing Java classes that deal with this sort of thing,...
3
1492
by: Michael Justin | last post by:
Mufasa wrote: Message Oriented Middleware might be an option too: http://en.wikipedia.org/wiki/Message-oriented_middleware There are many very good (including Open Source implementations) and most of them support .NET, so you can have active and passive components on the client. The message broker can notify the client about new
6
1306
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to properly implement that, is there a better way than using state management variables for each timer as i'm doing in the outline below? private string szProperty1 = null; // updated by class consumer
3
1051
by: King | last post by:
This is a new test for object persistency. I am trying to store the relationship between instances externally. It's not working as expected. May be I am doing it in wrong way. Any suggestions? import shelve class attrib(object): pass
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
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
9790
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
9779
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
9645
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
8645
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
5069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2612
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.