473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Inter-object event messaging (Mediator Pattern?) (very long sorry)

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
"participant". 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.Collections.Generic;
using System.Text;

With the class Dispatcher also using:
using System.Collections;
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.Publish("MyEvent", "Hello #" + i.ToString());
}
}

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

class C
{
private string name;

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

public void OnMyEvent(object appEventParams)
{
string textReceived = (string)appEventParams;
Console.WriteLine(textReceived + " received in " + name);
}

}

public delegate void AppEventHandler(object appEventParams);

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

public static void Subscribe(string appEvent, AppEventHandler
appEventHandler)
{
Subscription(appEvent, appEventHandler, true);
}

public static void Unsubscribe(string appEvent,
AppEventHandler appEventHandler)
{
Subscription(appEvent, appEventHandler, false);
}

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

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

list = (ArrayList)registry[appEvent];
list.Add(appEventHandler);
}
else
{
if (registry[appEvent] != null)
{
list = (ArrayList)registry[appEvent];
list.Remove(appEventHandler);
}
}
}

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

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

for (int i = 0; i < list.Count; i++)
{
appEventHandler = (AppEventHandler)list[i];
if (appEventHandler != 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 24 '07 #1
1 1710
I don't have time to read through and think about your whole question but a
working mediator pattern implementation in C# is to be found here:
http://www.dofactory.com/Patterns/Pa...or.aspx#csharp - don't know
if it does exactly the same your code does.
Maybe it will clarify something you need to have clarified.

<ha*****@yahoo.comwrote in message
news:11**********************@s33g2000prh.googlegr oups.com...
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
"participant". 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.Collections.Generic;
using System.Text;

With the class Dispatcher also using:
using System.Collections;
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.Publish("MyEvent", "Hello #" + i.ToString());
}
}

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

class C
{
private string name;

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

public void OnMyEvent(object appEventParams)
{
string textReceived = (string)appEventParams;
Console.WriteLine(textReceived + " received in " + name);
}

}

public delegate void AppEventHandler(object appEventParams);

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

public static void Subscribe(string appEvent, AppEventHandler
appEventHandler)
{
Subscription(appEvent, appEventHandler, true);
}

public static void Unsubscribe(string appEvent,
AppEventHandler appEventHandler)
{
Subscription(appEvent, appEventHandler, false);
}

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

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

list = (ArrayList)registry[appEvent];
list.Add(appEventHandler);
}
else
{
if (registry[appEvent] != null)
{
list = (ArrayList)registry[appEvent];
list.Remove(appEventHandler);
}
}
}

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

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

for (int i = 0; i < list.Count; i++)
{
appEventHandler = (AppEventHandler)list[i];
if (appEventHandler != 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 24 '07 #2

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

Similar topics

2
by: cppaddict | last post by:
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...
7
by: Mrkrich | last post by:
I have one procedure that will take very long time before it finishs. During its running, I provide users a button to cancel this process if they don't want it to run anymore. I have one varible...
0
by: Philippe | last post by:
Hi, I've got troubles with event messaging between 2 classes. In a WinForm, a DataGrid has a ComboBox. I need to use the SelectedIndexChanged of the cbo to perform some DB updates. ComboBox is...
0
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
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,...
3
by: Rich Denis | last post by:
Hello, I am in need of assistance trying to figure out how to 'Unit Test' my Event Based Async Pattern (http://msdn2.microsoft.com/e7a34yad.aspx) web service calls. Specifically how to test the...
3
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I need to build an event-based asynchronous pattern (around a send/receive messaging API). Is there a step-by-step guidance about how to write code for the EBAP ? Does any book cover this theme...
1
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...
12
by: Ahmad Jalil Qarshi | last post by:
Hi, I have an integer value which is very long like 9987967441778573855. Now I want to convert it into equivalent Hex value. The result must be 8A9C63784361021F I have used...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.