Tuesday, February 19, 2008

Real life example of Factory Method Design Pattern

Hi,

When i read any design pattern i would like to have some real life example to get that easily. Today i was going throug Factory Method design pattern from
http://www.dofactory.com/Patterns/PatternFactory.aspx and looking for one good example. Finally i found a good one and now i am sharing that with you.

A non-software example that demonstrates this pattern would be a restaurant. Restaurants serve a variety of items which they list in the menu. The restaurant's waiter provides a menu to the customer and, based on the items available in the Menu, the customer selects the items and orders them. These selected items are prepared and served to the customer by the waiter.
If you map this non-software example against the factory design pattern, we can say:
Items, as the subclasses, are served to the customer by a waiter and its preparation is hidden from the customer.
The waiter, as the Factory class, takes the customer's order based on the items available in the menu and serves them.
The customer, as the Client code, orders the items and does not see how the items are prepared.

Hope this will help you to understand the implementation of this pattern.

/Zaq

Sunday, February 17, 2008

Get the list of bugs that you have fixed during development. [Part-III]

And finally here is your implementation.

using System;
using System.Collections.Generic;
using System.Text;
using FindBug.Entity;

namespace FindBug
{
public class Program
{
static void Main(string[] args)
{
MyCodeWitBugFixing mm = new MyCodeWitBugFixing();
Console.WriteLine("Calling SumTwoDigit(7, 3). Result: {0}", mm.SumTwoDigit(7, 3));
// get the member information and use it to retrieve the custom attributes
System.Reflection.MemberInfo inf = typeof(MyCodeWitBugFixing);

object[] attributes;
attributes = inf.GetCustomAttributes(typeof(BugInformation), false);

// iterate through the attributes, retrieving the properties
foreach (Object attribute in attributes)
{
BugInformation bfa = (BugInformation)attribute;
Console.WriteLine("\nBugID: {0}", bfa.BugID);
Console.WriteLine("Programmer: {0}", bfa.Programmer);
Console.WriteLine("Date: {0}", bfa.Date);
Console.WriteLine("Comment: {0}", bfa.Comment);
}
}
}
}

Put all code together and test it. Hope you will like it.

Part-I, Part-II

Get the list of bugs that you have fixed during development. [Part-II]

This is your BugInformation attribute class.

using System;
using System.Collections.Generic;
using System.Text;

namespace FindBug.Entity
{
[AttributeUsage(AttributeTargets.Class AttributeTargets.Constructor
AttributeTargets.Field AttributeTargets.Method
AttributeTargets.Property, AllowMultiple = true)]
public class BugInformation : System.Attribute
{
//private member data
private int bugID;
private string comment;
private string date;
private string programmer;

public BugInformation(int bugID, string programmer, string date)
{
this.bugID = bugID;
this.programmer = programmer;
this.date = date;
}

//Named Param
public string Comment
{
get
{
return comment;
}
set
{
comment = value;
}
}

//Positional Param
public int BugID
{
get
{
return bugID;
}
}

public string Programmer
{
get
{
return programmer;
}
}

public string Date
{
get
{
return date;
}
}
}
}


Here is your Class, for which you fix bugs.

using System;
using System.Collections.Generic;
using System.Text;
using FindBug.Entity;

namespace FindBug
{

[BugInformation(121, "Mamun", "01/03/08")]
[BugInformation(107, "Zaq", "01/04/08", Comment = "Fixed one error")]
public class MyCodeWitBugFixing
{
public MyCodeWitBugFixing()
{
}

public int SumTwoDigit(int i, int j)
{
return i + j;
}

public int DeductTwoDigit(int i, int j)
{
return i - j;
}
}
}

Part-I, Part-III

Get the list of bugs that you have fixed during development. [Part-I]

The way I will describe is a use of Custom Attribute. Suppose, your organization wants to keep track of bug fixes and you have your own BugTracking system or a database to keep all of your bugs, but you’d like to attach your comment on specific code that you have been fixed and get them as a list to put in database whenever you like. This example describe how to attach bug information to a class and get them by your code.

Generally when you fixed a bug then add comments to your code like this:
// Bug 323 fixed by Mamun on 17/2/2008.

This would make it easy to see in your source code, but there is no enforced connection to Bug 323 in the database. A custom attribute might be just what you need. You would replace your comment with something like this:
[BugInformation(323,"Mamun","17/2/2008") Comment="Off by one error"]

You could then write a program to read through the metadata to find these bug-fix notations and update the database. The attribute would serve the purposes of a comment, but would also allow you to retrieve the information programmatically through tools you'd create.

Part-II, Part-III