Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

Tuesday, August 10, 2010

Factory Pattern


                  In the object oriented programming there are numbers of patterns, one type of pattern is Factory Patterns. When we have several classes and returning an instance of a single class depending on the type of the data is called Factory Pattern. Generally all the classes have a single parent class and each derived class contain same methods. Depending on the data, we fetch a single class object.

 
          In this figure, you can see there is a single Parent class which is derived by 3 Derived Class.  Here we have created a Factory Class, which contains a Factory Method (may contain parameter or not), which returns the parent class’s instance.
To see this figure we can say, the Factory class basically used to decide which derived class object should be initiate. It does not depend on the programmer.
Let me give you an example that will make it simple.
public abstract class NumberClas {  public abstract void Show(); }

Here NumberClass is a parent class, which contains a abstract method Show();
   public class EvenNumber : NumberClass
    {
        public EvenNumber()
        {}
       
       public override void Show()
        {
            Console.WriteLine("Even number.");
        }
    }

  public class OddNumber : NumberClass   {
   public OddNumber ()     {}   public override void Show()         {
  Console.WriteLine("Odd number.");       }
  }

The EvenNumber and OddNumber  are two derived classes, both have the same parent NumberClass and overriding the Show() method. To know the runtime which derived class’s object should be initialize I have created the FactoryClass. This class contains the GetObject(int number) method, which returns the correct object at the run time.

public class FactoryClass
 { 
     public NumberClass GetObject(int number)
     {
       
       if(number % 2 == 0)
         return new  EvenNumber();

else
         return new  OddNumber();
     } }

Now
 static void Main(string[] args)
  {
    //initialice the factory class object
   FactoryClass obFctoryClass = new FactoryClass();
  
   //initilize the base class object through the factory method
    NumberClass obNumberClass = obFactoryClass.GetObject(2);

    //call the derived class method
obNumberClass.Show();
  Console.WriteLine("\n******************");
   //initilize the base class object through the factory method obNumberClass = obFactoryClass.GetObject(3);
  //call the derived class method
obNumberClass.Show(); Console.ReadLine(); } }
Output is

Even number.
******************
Odd number.

Saturday, August 1, 2009

Decorator Pattern

There are numbers of way we used in the designing software application. These ways are called Patterns, and in the object-oriented programming, we use so many patterns. Decorator Pattern is a design pattern in the object-oriented programming that allows us to add the new functionality in the existing class dynamically. To understand let me give you a simple example. Suppose you have an application in ASP.NET and SQL Server.
In your business logic, you have written all you logics. i.e.

1. Insert()
2. Update()
3. Delete
4. Get()

In these logics, you have written the code according to your SQL Server fetching technique. You have used System.Data.SqlClient and open/close the connection with SQL Server and done all the insert/update/delete opertaions with SQL Server. You have also called this business logic objects into your UI layer. Now your application is fully dependent in SQL Server. The problem start now, you have a single application where you have done your all effort with the SQL Server and the new requirement is you have to also provide the support of the Microsoft Access with the same database and UI. Then what will you do? You have to create the another business logic with the Microsoft Access as well as have to change all the logic of UI layer because it is accessing the SQL Server business layer logic right now. It will force you rethink again in your design pattern. In such type of situation Decorator Pattern help you to get up. Let me make it simpler. Suppose, I have an asp.net .aspx page where I have to show all products information. Remember we have two databases one is in SQL Server, another is in Microsoft Access, and we have to write the code in a single UI. At the run time, we can decide which database we have to use. First, I will create an interface IProduct. (Here I am using a console application.)

public interface IProduct
{
bool Insert(int id, string name);
bool Update(int id, string name);
bool Delete(int id);
string GetRecords();
}

Then I have to create a Product class which will contains all the operations with SQL Server database. This Product class will inherits the IProduct interface.


public class ProductSQLServer : IProduct 
{
int id = 10;
string name = "SQL Server";

#region IProduct Members

public bool Insert(int id, string name)
{

//insert logic

return true;

}

public bool Update(int id, string name)
{
//upate logic
return true;
}

public bool Delete(int id)
{
//delete login
return true;
}

public string GetRecords()
{
return "sql server : id=" + id + " name=" + name ;
}

#endregion

}


public class ProductMicrosoftAccess : IProduct
{
int id = 10;
string name = "MicrosoftAccess";

#region IProduct Members

public bool Insert(int id, string name)
{
//insert into the microsoft access
return true;
}

public bool Update(int id, string name)
{
//update in the microsoft access
return true;
}

public bool Delete(int id)
{
//delete from the microsoft access
return true;
}

public string GetRecords()
{
return "MicrosoftAccess : id=" + id + " name=" + name ;
}

#endregion
}


Now in the UI layer we can decide in the run time what type of object (SQL Server/Access) need to be accessed.

In the view product.aspx page I can decide whether I want to create the object of ProductMicrosoftAccess class or ProductSQLServer class.
For that you have to use the IProduct interface


static void Main(string[] args)
{
IProduct  obOperation = new ProductMicrosoftAccess();

Console.WriteLine(obOperation.GetRecords());

obOperation = new ProductSQLServer();

Console.WriteLine(obOperation.GetRecords());


Console.ReadLine();
}


You can choose the type of database by setting the key in your web.config file or any configuration file.




Download :