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:
<add prefix="http" type="System.Net.HttpRequestCreator, .../>
<add prefix="https" type="System.Net.HttpRequestCreator, .../>
<add prefix="file" type="System.Net.FileWebRequestCreator, .../>
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...