Factory and Abstract Factory…….

  • Factory method is a single method, and an abstract factory is an object.
  • Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition.
  • Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.
  • Factory Method pattern exposes a method to the client for creating the object whereas in case of Abstract Factory they expose a family of related objects which may consist of these Factory methods.
  • Factory Method pattern hides the construction of single object where as Abstract factory method hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.
  • Abstract Factory pattern uses composition to delegate responsibility of creating object to another class while Factory design pattern uses inheritance and relies on derived class or sub class to create object.
  • The idea behind the Factory Method pattern is that it allows for the case where a client doesn’t know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.

Factory Pattern Example

 

public interface IVehicle
 {
     void Start();
     void Drive();
     void Stop();
 }
 
 public class Car : IVehicle
 {
 
     public void Start()
     {
         Console.WriteLine("Car Started");
     }
 
     public void Drive()
     {
         Console.WriteLine("Car Driving");
     }
 
     public void Stop()
     {
         Console.WriteLine("Car Stopped");
     }
 }
 
 public class Suv : IVehicle
 {
 
     public void Start()
     {
         Console.WriteLine("Suv Started");
     }
 
     public void Drive()
     {
         Console.WriteLine("Suv Driving");
     }
 
     public void Stop()
     {
         Console.WriteLine("Suv Stopped");
     }
 }
 
 public enum VehicleType
 { 
     Car,
     Suv
 }
 public class VehicleFactory
 {
     public static IVehicle GetVehicle(VehicleType vehicleType)
     {
         IVehicle vehicle;
         switch (vehicleType)
         {
             case VehicleType.Car:
                 vehicle = new Car();
                 break;
             case VehicleType.Suv:
                 vehicle = new Suv();
                 break;
             defaultreturn null;
         }
         return vehicle;
     }
 }

Client Consuming Code looks like

class Program
{
    static void Main(string[] args)
    {
        IVehicle vehicle = VehicleFactory.GetVehicle(VehicleType.Car);
        vehicle.Start();
        vehicle.Drive();
        vehicle.Start();
        Console.ReadLine();
    }
}

Source Code can be downloaded from Here

Abstract Factory Example

public class Car : IVehicle
{
 
    public virtual void Start()
    {
        Console.WriteLine("Car Started");
    }
 
    public virtual void Drive()
    {
        Console.WriteLine("Car Driving");
    }
 
    public virtual void Stop()
    {
        Console.WriteLine("Car Stopped");
    }
}
 
public class HondaAccord : Car
{
    public override void Start()
    {
        Console.WriteLine("Honda Accord Started");
    }
 
    public override void Drive()
    {
        Console.WriteLine("Honda Accord Driving");
    }
 
    public override void Stop()
    {
        Console.WriteLine("Honda Accord Stopped");
    }
}
 
public class HondaCrv : Suv
{
    public override void Start()
    {
        Console.WriteLine("Honda Crv Started");
    }
 
    public override void Drive()
    {
        Console.WriteLine("Honda Crv Driving");
    }
 
    public override void Stop()
    {
        Console.WriteLine("Honda Crv Stopped");
    }
}
 
public class ToyotaCorolla : Car
{
    public override void Start()
    {
        Console.WriteLine("Toyota Corolla Started");
    }
 
    public override void Drive()
    {
        Console.WriteLine("Toyota Corolla Driving");
    }
 
    public override void Stop()
    {
        Console.WriteLine("Toyota Corolla Stopped");
    }
}
 
public class ToyotaRav4 : Suv
{
    public override void Start()
    {
        Console.WriteLine("Toyota Rav4 Started");
    }
 
    public override void Drive()
    {
        Console.WriteLine("Toyota Rav4 Driving");
    }
 
    public override void Stop()
    {
        Console.WriteLine("Toyota Rav4 Stopped");
    }
}
 
public class Suv : IVehicle
{
 
    public virtual void Start()
    {
        Console.WriteLine("Suv Started");
    }
 
    public virtual void Drive()
    {
        Console.WriteLine("Suv Driving");
    }
 
    public virtual void Stop()
    {
        Console.WriteLine("Suv Stopped");
    }
}
public abstract class VehicleFactory
{
    public abstract IVehicle GetVehicle(VehicleType vehicleType);
 
    public static VehicleFactory GetFactory(MakeType make)
    {
        VehicleFactory vehicleFactory;
        switch (make)
        {
            case MakeType.Honda:
                vehicleFactory = new HondaFactory();
                break;
            case MakeType.Toyota:
                vehicleFactory = new ToyotaFactory();
                break;
            defaultreturn null;
        }
        return vehicleFactory;
    }
    
}
 
public class ToyotaFactory : VehicleFactory
{
    public override IVehicle GetVehicle(VehicleType vehicleType)
    {
        IVehicle vehicle;
        switch (vehicleType)
        {
            case VehicleType.Car:
                vehicle = new ToyotaCorolla();
                break;
            case VehicleType.Suv:
                vehicle = new ToyotaRav4();
                break;
            defaultreturn null;
        }
        return vehicle;
    }
}
 
public class HondaFactory : VehicleFactory
{
    public override IVehicle GetVehicle(VehicleType vehicleType)
    {
        IVehicle vehicle;
        switch (vehicleType)
        {
            case VehicleType.Car:
                vehicle = new HondaAccord();
                break;
            case VehicleType.Suv:
                vehicle = new HondaCrv();
                break;
            defaultreturn null;
        }
        return vehicle;
    }
}
 
public enum VehicleType
{
    Car,
    Suv
}
 
public enum MakeType
{
    Toyota,
    Honda
}

Client Consuming Code looks like

class Program
{
    static void Main(string[] args)
    {
        VehicleFactory factory = VehicleFactory.GetFactory(MakeType.Honda);
        IVehicle vehicle = factory.GetVehicle(VehicleType.Car);
        vehicle.Start();
        vehicle.Drive();
        vehicle.Stop();
        Console.ReadLine();
    }
}

Source Can be downloaded from Here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s