Sunday, January 16, 2005

On Software Patterns

Recently..I began to wonder about "Software Design Patterns".I began to wonder ,what are they based on?.Arent they afterall someone's experiences.For instance lets say we are talking about designs in civil engineering.The design, the engineer in that domain uses ,makes, are based on the solid foundations of mathematics.Lets assume that he has to construct a door,he will defintely follow a pattern based on usability and proper dimensions,he will know that the door has to be of 'x' feet high and 'y' feet wide(the maths of numbers again!!!).Proved and consistently proved mathematical formulas are the basis for the designer there, to determine what the dimensions of his structure are going to be.On the contrary when a software designer steps out to design something,what has he, to base his design on ?.Today I know that a code i had implemented long ago ,actually is a pattern..and I had done it without even having been aware of the fact that i had implemented the ADAPTER pattern.And to add to it,in most of the cases it has been found out that the design is more or less incomplete ,without its implementation in code bringing about some changes in the design again.And to think of it more than 60% of a project life cycle goes into planning and designing.So are we still very far from knowing what should be foundations of our designs ?.

Turning my thoughts elsewhere,I have been lately wondering of a particular use of Aspect oriented programming or Adaptive programming(call it AOP for simplicity).Its been a well known fact the features of a AOP could be used for logging et cetra..It is also a well realized fact that, database connections are a precious resource,and should be used as prudently as possible.In fact the shorter you hold the DB connection the better and the shortest we can try to hold a DB connection as programmers, could be at a method level.It is here that we could use the features of AOP to turn on the DB connection(get a hook to it) at the entry point of the method and release the same at the exit of the method.This sure follows a pattern,so we can as well, call this a pattern.
Links you may find useful :
Martin Fowlers ideas
IBM DeveloperWorks


Sunday, January 02, 2005

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) ;

/// Class is an end appliction that subscribes for
/// an event and also invokes the publisher for publishing an event.
///

class RunEvent
{
/// The main entry point for the application.
[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) ;

}
}

/// descibes an user defined event
class Events : EventArgs
{
EventsToGenerate evt ;
public Events(EventsToGenerate evt):base()
{
this.evt = evt ;
}
public EventsToGenerate Evt
{
get
{
return this.evt ;
}
}
}

/// It is the event publisher
class EventGenerator
{
EventBroker broker = EventBroker.GetEventBroker() ;
public void Publish( EventsToGenerate evt)
{
broker.Delegate(Event.EventsToGenerate.NewEvent) ;
}
}

/// Defines the subscriber logic
class EventConsumer
{
public void Subscribe(EventsToGenerate evt)
{
if(evt == EventsToGenerate.NewEvent)
{
EventBroker broker = EventBroker.GetEventBroker() ;
Notify notify = new Notify(Handler) ;
broker.EventDistributor(notify) ;
}
}

/// callback function which will be called by the event broker when events of a
///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 provides brokerage between the publishers and the subscribers
///

class EventBroker
{
private static EventBroker broker ;

/// singleton event broker
/// single instance of event broker
public static EventBroker GetEventBroker()
{
if(broker == null)
{
broker = new EventBroker() ;
return broker ;
}
else
return broker ;
}

public event Notify notifyConsumers ;

/// notify the subscribers
/// the event to be notified to the subscribers
public void Delegate(EventsToGenerate evt)
{
this.notifyConsumers(this,evt) ;
}
/// subscribers use this to register the callbacks
/// 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!!!!!