Implementing Delegation-Event Model in C#, similar to that of Delegation-Event model of Java


It is next to impossible for any Java Developer to ignore .NET framework and C# language. C# is in many ways similar to Java.

The best way for any Java Developer to learn C# wouldbe to start converting their small programs written in Java to C#.

This is how I learnt about .NET framework and C# and I still do the same.

This time I thought of implementing Delegation-Event model concept from Java to C# andsharing my experience with other fellow Java and C# developers.

Delegation-Event model :
This concept of Delegation-Event model is very simple wherein a source generatesan event and sends it to all the registered listeners.

To achieve above objective, follow these steps :

1. Create an Event called CustomEvent

//File Name : CustomEvent.cs
using System;

//CustomEvent is an user defined event, which extends
//EventArgs class. The Constructor of this class
//takes eventSource of type Object as parameter.
//eventSource parameter represents the object, which
//would generate this event

public class CustomEvent : EventArgs
{
public CustomEvent(Object eventSource): base(eventSource) {}
}

2. Create a Listener interface called ICustomEventListener.
All the objects interested in the notification of CustomEvent would implement this Interface.

//File Name : CustomEventSource.cs
using System;

//Interface ICustomEventListener has one method
//ActionPerformed to be implemented by all the objects
//interested in receiving notification for event CustomEvent.

public interface ICustomEventListener
{
//Action performed on the event CustomEvent
void ActionPerformed(CustomEvent ce);
}

3. Create an event source called CustomEventSource, for registering all the listeners and notifying them about the CustomEvent.

//File Name : CustomEventSource.cs
using System;
using System.Collections;

//Class CustomEventSource generates CustomEvent and notifies all
//listeners interested in receiving CustomEvent. It registers all
//listener's in a ArrayList and iterates the ArrayList to notify
//all its member

public class CustomEventSource
{
// Create and initialize a new ArrayList.
ArrayList listenerAL = new ArrayList();

public CustomEventSource() {}

//Add listener to ArrayList
public void AddCustomEventListener(ICustomEventListener ice )
{
listenerAL.Add(ice);
}

//Remove listener from ArrayList
public void RemoveCustomEventListener(ICustomEventListener ice )
{
listenerAL.Remove(ice);
}

//Notify all the listener
public void FireEvent()
{

CustomEvent ce = new CustomEvent(this);
IEnumerator myEnumerator = listenerAL.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
ICustomEventListener icel = (ICustomEventListener)myEnumerator.Current ;
icel.ActionPerformed(ce);
}

}
}

4. Create a CustomEventReceiverEx, to show the working of Delegation-Event model.
This class implements the method ActionPerformed of the Interface ICustomEventListener.

//File Name : CustomEventReceivedEx.cs

using System;

//A working example, shows how object CustomEventReceiverEx gets
//a notification for CustomEvent. In order to get notification, listener
//must implement ICustomEventListener interface.

public class CustomEventReceiverEx : ICustomEventListener
{
//Action performed on the event
public void ActionPerformed(CustomEvent ce)
{

Console.WriteLine("Got a event notification.");
Console.WriteLine("Type of this event is <<" + ce.ToString() + ">>.");
Console.WriteLine("Delegate-Event model is so elegant and simple to use.");
Console.ReadLine();
}

public CustomEventReceiverEx() {}

public static int Main(string[] args)
{
CustomEventSource es = new CustomEventSource();
CustomEventReceiverEx ere = new CustomEventReceiverEx();
es.AddCustomEventListener(ere);
es.FireEvent();
return 0;
}
}

5: Compile above source code as
csc /recurse:*.cs

6: Run CustomEventReceiverEx.exe program to see the output in console window as

Got a event notification.
Type of this event is <<customevent>>.
Delegate-Event model is so elegant and simple to use.</customevent>

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!