Geeks With Blogs
Dheeman Dutta Just Another Blog.....
In the Factory Pattern we were creating objects of different types (viz. different car types Ambassador, Ford etc.) using the pattern. But sometimes we may need to create same object , but each having a different creational process, i.e. though they are objects of the same type they may have different attributes initialized when they are created based on our requirement. There comes the need of Abstract Factory Pattern.
 
Here we design an interface which acts as the contract for the AbstractFactory
 
interface IAbstractfactory
            {
                  Hashtable Carattributes();
            }
 
We are creating two types of the same car , but with different initialized attributes. These classes implement the interface IAbstractfactory.
 
      class EconomicCAR:IAbstractfactory
            {
                  public Hashtable Carattributes()
                  {
                        Hashtable eco = new Hashtable();
                        eco["Price"]="1000";
                        eco["colorsAvailable"]=3;
                        return eco;
 
                  }
            }
 
            class BusinessCAR:IAbstractfactory
            {
                  public Hashtable Carattributes()
                  {
                        Hashtable business = new Hashtable();
                        business["Price"]="20000";
                        business["colorsAvailable"]=2;
                        business["isAvailableinCountry"]=true;
                        return business;
 
                  }
 
 
 
You can clearly see that both car types have different attributes. But as mentioned before
Both of these actually are same type of projects.
 
Now we create the CarBase class as the base class of the cars.
 
class CarBase
            {
                  IAbstractfactory _abstractfactory;
                  Hashtable _carattributes;
 
                  public IAbstractfactory Factory
                  {
                        get
                        {
                              return _abstractfactory;
                        }
                        set
                        {
                              _abstractfactory=value;
                        }
                  }
 
                  public Hashtable CarAttributes
                  {
                        get
                        {
                              _carattributes=Factory.Carattributes();
                              return _carattributes;
                        }
                       
                  }
            }
 
This class contains the a get/set property of type interface IAbstractFactory. This enables us to send any type of class at runtime, that implements the interface IAbstractFactory. The CarAttributes property enables only a get and gets the Carattributes of the passed-in _abstractfactory object. This way we do not need to have any if-else constructs while creating an object.
 
Now we come to the cars that actually inherit from the CarBase class.
 
class CarEconomic:CarBase
            {
                  public CarEconomic()
                  {
                        Factory= new EconomicCAR ();
                  }
            }
 
            class CarBusiness:CarBase
            {
                  public CarBusiness()
                  {
                        Factory= new BusinessCAR ();
                  }
            }
 
CarEconomic and CarBusiness are two class we would actually need to create at runtime. Both of these inherit from CarBase. In their corresponding constructors they pass the required object(that implements IAbstractFactory) to the Factory property of the CarBase class.
 
 
 
 
In main
 
static void Main(string[] args)
            {
                  CarBusiness _business= new CarBusiness();
                 
            foreach(DictionaryEntry e in _business.CarAttributes )
                  {
                        Console.WriteLine(e.Key + ": " + e.Value);
                       
                  }
                 
                  Console.WriteLine();
 
                  CarEconomic _economic = new CarEconomic();
 
            foreach(DictionaryEntry e in _economic.CarAttributes )
                  {
                        Console.WriteLine(e.Key + ": " + e.Value);
                  }
 
                  Console.ReadLine();
            }
 
 
As seen from the code above, we are passing either CarBusiness or CarEconomic to create the corresponding object at runtime..
 
 
 
 Cheers...
Posted on Thursday, May 24, 2007 8:51 PM | Back to top


Comments on this post: Design patterns-part 2- Abstract Factory Pattern

# Links (5/25/2007)
Requesting Gravatar...
.NET XLINQ Introduction Part 3 Of 3 Ruby-Like Syntax in C# 3.0 MbUnit 2.4 RTM Read a file from an embedded
Left by Member Blogs on May 25, 2007 1:58 AM

Your comment:
 (will show your gravatar)


Copyright © Dheeman Dutta | Powered by: GeeksWithBlogs.net