Pragmatic Developer

Ali Özgür

Bookmark Blog

Add to Technorati Favorites

Google Talk

Chat with Ali Özgür

Purchase PragmaSQL from

Calendar

«  December 2008  »
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar

Tag Cloud

Don't show

    Authors

    Recent Comments

    Banners




    I believe Factory Pattern is one of the most known pattern in development community. Simply Factory Pattern states that we have a factory object which is used to create other objects. But GoF specification for the Factory Pattern is a little bit different. GoF book says that "We should define an interface for creating an object, but let subclasses decide which class to instantiate." 

    How you implement the Factory Pattern depends on your custom needs. 1) If you want to control object instantiation behaviour of your factory provide a static class with static methods or sealed factory class to your clients, else 2) if you want to keep your code closed for modification but open for extension follow the GoF way by providing an interface or abstract factory class.

    Method 1

    Download GoF_Patterns_Factory1.rar (18,45 kb)

    Now lets implement Factory Pattern for our Vehicle instantiation process so that we do not want our clients to have alternative factory implementation at all.

    001   //Abstract Vehicle class
    002    public abstract class Vehicle
    003    {
    004        public abstract string Description { get; }
    005    }
    006    
    007    //Concrete implementation
    008    public sealed class Car : Vehicle
    009    {
    010        internal Car() { }
    011        public override string Description
    012        {
    013            get { return "Car";}
    014        }
    015    }
    016
    017    //Concrete implementation
    018    public sealed class Helicopter : Vehicle
    019    {
    020        internal Helicopter() { }
    021        public override string Description
    022        {
    023            get { return "Helicopter"; }
    024        }
    025    }
    026
    027    //Concrete implementation
    028    public sealed class Jet : Vehicle
    029    {
    030        internal Jet() { }
    031        public override string Description
    032        {
    033            get { return "Jet"; }
    034        }
    035    }

    I've chosen to implement my factory as a static class with CreateVehicle static method. Here is the code

    001    public static class VehicleFactory
    002    {
    003        public static Vehicle CreateVehicle(string vehicleType)
    004        {
    005            string type = vehicleType.ToLower().Trim();
    006            switch (type)
    007            {
    008                case "car":
    009                    return new Car();
    010                case "helicopter":
    011                    return new Helicopter();
    012                case "jet":
    013                    return new Jet();
    014                default:
    015                    throw new Exception("Unsupported vehicle type!");
    016            }                
    017        }
    018    }

    As you can see our clients will not be able to change the Vehicle instantiation behaviour of our static factory class.

    001    static void Main(string[] args)
    002        {
    003            Vehicle vehicle = VehicleFactory.CreateVehicle("car");
    004            Console.WriteLine(vehicle.Description);
    005
    006            vehicle = VehicleFactory.CreateVehicle("jet");
    007            Console.WriteLine(vehicle.Description);
    008
    009            vehicle = VehicleFactory.CreateVehicle("helicopter");
    010            Console.WriteLine(vehicle.Description);
    011
    012            Console.ReadLine();
    013        }
     

    Method 2 

    Download GoF_Patterns_Factory2.rar (18,90 kb)

    If you want to enable your clients to modify object instantiation behaviour of the factory you must implement your factory the GoF way. Our abstract Vehicle class and different concerete implementations remain as the ones provided in Method 1. Here is our factory interface

    001    public interface IVehicleFactory
    002    {
    003        Vehicle CreateVehicle(string vehicleType);
    004    }

    Here a sample implementation of IVehicleFactory interface. Our clients may implement CreateVehicle method of our factory interface how they wish. 

    001   public class VehicleFactory:IVehicleFactory
    002    {
    003        #region IVehicleFactory Members
    004
    005        public Vehicle CreateVehicle(string vehicleType)
    006        {
    007            string type = vehicleType.ToLower().Trim();
    008            switch (type)
    009            {
    010                case "car":
    011                    return new Car();
    012                case "helicopter":
    013                    return new Helicopter();
    014                case "jet":
    015                    return new Jet();
    016                default:
    017                    throw new Exception("Unsupported vehicle type!");
    018            }
    019        }
    020
    021        #endregion
    022    }

    001    static void Main(string[] args)
    002        {
    003            VehicleFactory factory = new VehicleFactory();
    004            Vehicle vehicle = factory.CreateVehicle("car");
    005            Console.WriteLine(vehicle.Description);
    006
    007            vehicle = factory.CreateVehicle("jet");
    008            Console.WriteLine(vehicle.Description);
    009
    010            vehicle = factory.CreateVehicle("helicopter");
    011            Console.WriteLine(vehicle.Description);
    012
    013            Console.ReadLine();
    014        }


    Posted in: C# , GoF Patterns  Tags:

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Comments


    October 3. 2008 15:53
    pingback
    Pingback from sehajpal.com

    Ashish Sehajpal

    http://www.sehajpal.com/blog/?p=91

    Add comment


    (Will show your Gravatar icon)  

      Country flag

    [b][/b] - [i][/i] - [u][/u]- [quote][/quote]



    Live preview


     
    December 5. 2008 04:35

    no site