Saturday, November 20, 2004

Design patterns

'Design patterns' are a kind of langauge for communication of ideas for solving design problems. I would discuss the common design patterns here whenever possible. Many of the design patterns have been used in .Net framework class library. Every pattern i discuss would possibly include an example from the .Net FCL. This post will discuss the 'Factory pattern':
Factory Design Pattern
(Creational Pattern)

Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Many a times the client needs flexibility in deciding and instantiating a group of related objects from a set of candidates. Consider a simple example: Assume the car factory can assemble two types of cars: SCODA and MARUTI. The customer specifies what type of car he wants. Given these requirements we can model our program in this way:
Customer Interface – Generic Car Assembler – Specific Car Assemblers

The Customer Interface obtains the choice from the customer and sends it to Generic Car Assembler component. Depending on the choice the Generic Car Assembler component invokes the appropriate Car Assembler and delivers the car to the customer.

class diagram for Factory pattern

A good .Net FCL (Framework Class Library) example is System.Net.WebRequest:
WebRequest serves as a convenient base class for the .NET Framework's request/response model for accessing data from the Internet. This class encapsulates the details of connecting to the server, sending the request, and receiving the response. This means that an application can participate in request/response transactions in a protocol-agnostic manner using instances of the WebRequest class, while protocol-specific classes derived from WebRequest carry out the details of the request.
The static factory method WebRequest.Create creates protocol-specific descendants of WebRequest using the value of the URI passed in as argument. For example, when a URI beginning with "http://" is passed, an HttpWebRequest object is returned; when a URI beginning with "file://" is passed, a FileWebRequest object is returned.
By default, the .NET Framework supports "http://", "https://", and "file://" URI schemes. This is easily verified by looking at the section of the machine.config file in the .NET Framework installation's CONFIG directory:
<webRequestModules>
<add prefix="http" type="System.Net.HttpRequestCreator, .../>
<add prefix="https" type="System.Net.HttpRequestCreator, .../>
<add prefix="file" type="System.Net.FileWebRequestCreator, .../>
</webRequestModules>
This section implies that System.Net.HttpRequestCreator is responsible for creating WebRequest objects for request URIs beginning with "http" and "https", and System.Net.FileWebRequestCreator is responsible for creating WebRequest objects for request URIs beginning with "file". Both of these classes implement the System.Net.IWebRequestCreate interface, which contains only one public method: WebRequest Create(Uri uri);

Next time some other pattern...

Friday, November 19, 2004

a simple uml tool

I was tired of using UML modeling tools which force you to do it their own way. Using paper and pen is the best option, but that means to store it you have to use a scanner… pretty bad… So I wished to get a UML tool which satisfied these simple needs of mine:
1. Should be simple and light weight
2. Should not force you do things
3. Need not include code generation
4. Ideally should be free of cost :)

It was pretty bad until I discovered Argo UML - the open source UML modeling tool. It meets all my needs mentioned above. You can export the diagrams as GIF images which I found very useful.

So if you are tired of using complex UML modeling tools, go head download Argo UML at: http://argouml.tigris.org/

Wednesday, November 17, 2004

C# Events and Delegates

Difference between C# events and delegates
Recently i was wondering about the difference between C# delegates and events. For me events seemed to be a very thin wrapper around delegates. I have been trying to find out the advantage that the keyword "event" adds. I have to explore more on this aspect. But here i present a simple example containing a publisher, a subscriber, and an event broker.

using System;
namespace Test
{
class Publisher
{
private EventBroker evtBroker;
public void RegisterWithBroker(EventBroker evtBroker)
{
this.evtBroker = evtBroker;
}

public void Publish(string message)
{
this.evtBroker.Notify(message); // <---- statement 1
//this.evtBroker.handler(message); // <---- statement 2
}
}

class EventBroker
{
public delegate void onEvent(string message);
public event onEvent handler; //<---- statement 3
//public onEvent handler; //<--- statement4-a simple delegate

//will be used by puiblishers
public void Notify(string message)
{
handler(message);
}

// //this call is used to subscribe
public void RegisterSubscriber(onEvent handler)
{
this.handler += handler;
}
}

class Subscriber
{
public void ReceivedMessage(string message)
{
Console.WriteLine("Received Message: "+message);
}
}

class Test
{
[STAThread]
static void Main(string[] args)
{
EventBroker broker = new EventBroker();
Subscriber sub1 = new Subscriber();
Subscriber sub2 = new Subscriber();

broker.RegisterSubscriber(new EventBroker.onEvent(sub1.ReceivedMessage));
broker.RegisterSubscriber(new EventBroker.onEvent(sub2.ReceivedMessage));


Publisher pub = new Publisher();
pub.RegisterWithBroker(broker);

pub.Publish("Hello to all subscribers");
}
}
}

The important point to note here is that if you comment out 'statement1' and un-comment 'statement2'. Then you would get a complier error that the event handler can be invoked (raised) only inside the 'EventBroker' which defines it. 'Statement 4' defines the delegate, now if i comment out statement 1 and 3 and un-comment statement 2 and 4. The code works perfectly.

The event i believe has an extra constrain that it can only be invoked from within the class which defines it. Hence the class defining an event should define a method like "Notify" to facilitate other classes raise the event. I believe there are other differences...i just hit upon this one..will look for others...