Sample Event Brokerage
Sometime back I believe ,I said that I would post some code that demoed the process of Event Brokerage.I am putting the same in this blog.Having been so much hands on in .net throughout,I could but possibly put the code ,only in C# :) . I assume many are familiar with the language derived from Java and C++.It has this feature of delegates that so well suits our needs.Delegates are much similar to function pointers in C++ in terms of functionality and help in implementing callbacks.Note how the publisher and the subscriber have been isolated in the code,this imlementation is based on the observer pattern(for more information on Observer Pattern design click here) .Much of the code has been explained in the commenting itself.It can also be easily followed of course!!!.The code requires at least version 1 of .net framework to execute.Here we go..........
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Event
{
//dummy events to be used by the subscribers
public enum EventsToGenerate {NewEvent}
//delegate used to pass the callback in the subscriber
//to the event broker
public delegate void Notify(object sender,EventsToGenerate evt) ;
///
/// an event and also invokes the publisher for publishing an event.
///
class RunEvent
{
///
[STAThread] static void Main(string[] args)
{
//subscribe for a particular event
EventConsumer consumer = new EventConsumer() ;
consumer.Subscribe(EventsToGenerate.NewEvent) ;
//now publish a particular event type and
//see if the consumer is called
EventGenerator generateEvent = new EventGenerator() ; generateEvent.Publish (EventsToGenerate.NewEvent) ;
}
}
///
class Events : EventArgs
{
EventsToGenerate evt ;
public Events(EventsToGenerate evt):base()
{
this.evt = evt ;
}
public EventsToGenerate Evt
{
get
{
return this.evt ;
}
}
}
///
class EventGenerator
{
EventBroker broker = EventBroker.GetEventBroker() ;
public void Publish( EventsToGenerate evt)
{
broker.Delegate(Event.EventsToGenerate.NewEvent) ;
}
}
///
class EventConsumer
{
public void Subscribe(EventsToGenerate evt)
{
if(evt == EventsToGenerate.NewEvent)
{
EventBroker broker = EventBroker.GetEventBroker() ;
Notify notify = new Notify(Handler) ;
broker.EventDistributor(notify) ;
}
}
///
///particular type occur
///
/// this can be used to determine the source of the event
/// event that occurred
protected void Handler(object sender,EventsToGenerate evt)
{
if(evt == EventsToGenerate.NewEvent)
MessageBox.Show("Handled the NewEvent! My God!!!") ;
else
{
MessageBox.Show("Couldnt get the NewEvent! My God!!!") ;
}
}
}
///
///
class EventBroker
{
private static EventBroker broker ;
///
///
public static EventBroker GetEventBroker()
{
if(broker == null)
{
broker = new EventBroker() ;
return broker ;
}
else
return broker ;
}
public event Notify notifyConsumers ;
///
/// the event to be notified to the subscribers
public void Delegate(EventsToGenerate evt)
{
this.notifyConsumers(this,evt) ;
}
///
/// with the event broker so that the event broker
/// can notify the callbacks whenever events occur
///
///
public void EventDistributor(Notify callback)
{
notifyConsumers += new Notify(callback);
}
}
}
Voila!!!!!
0 Comments:
Post a Comment
<< Home