Friday 10 January 2014

Abstract Factory design pattern - implementation

In this article we are going to see the how to implement the Abstract Factory design pattern in code, Abstract Factory design is creating a abstract class and a abstract method  which will return the required instance based on the input parameter type and create a Factory by the static method.

Sample Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactoryDesign
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Abstract Factory Design ...");

            AnimalFactory landAnimalfactory = AnimalFactory.GetAnimalFactory(FactoryType.LandAnimalFactory);
            Console.WriteLine();
            Console.WriteLine("***** Land Animal *****");           
            Console.WriteLine(landAnimalfactory.GetAnimal(AnimalType.Lion).Speak());
            Console.WriteLine(landAnimalfactory.GetAnimal(AnimalType.Dog).Speak());           
            Console.WriteLine();
            AnimalFactory seaAnimalfactory = AnimalFactory.GetAnimalFactory(FactoryType.SeaAnimalFactory);
           
            Console.WriteLine("***** Sea Animal ******");           
            Console.WriteLine(seaAnimalfactory.GetAnimal(AnimalType.Octopus).Speak());
            Console.WriteLine(seaAnimalfactory.GetAnimal(AnimalType.Whale).Speak());
            Console.WriteLine();
            Console.WriteLine(".......");
            Console.Read();

        }
    }
}


Output:
Abstract Factory Design ...

***** Land Animal *****
Roar
Bark

***** Sea Animal ******
Whack
No Voice

.......


Now in this sample we take a Interface IAnimal which have a method speak, now this is imlpement for many animals cow, dog, lion, shark, octopus.
  
    public interface IAnimal
    {
        string Speak();
    }


    class Dog:IAnimal
    {
        public string Speak()
        {
            return "Bark";
        }
    }

    class Lion:IAnimal
    {
        public string Speak()
        {
            return "Roar";
        }
    }

    class Cat:IAnimal
    {
        public string Speak()
        {
            return "Meow";
        }
    }

    class Octopus:IAnimal
    {
        public string Speak()
        {
            return "Whack";
        }
    }

    class Whale:IAnimal
    {
        public string Speak()
        {
            return "No Voice";
        }
    }


Declare the Enum Types


    public enum AnimalType
    {
        Lion,
        Dog,
        Cat,
        Whale,
        Octopus,

    }

    public enum FactoryType
    {
        LandAnimalFactory,
        SeaAnimalFactory
   
    }



Now Let we create a Abstract Factory class with static method return the child instance type ,based on the input. Create a Two derived class from the abstract factory as LandAnimalFactory and SeaAnimalFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactoryDesign
{
    public abstract class AnimalFactory
    {
        public abstract IAnimal GetAnimal(AnimalType animalType);

        public static AnimalFactory GetAnimalFactory(FactoryType factoryType)
        {
            switch(factoryType)
            {
                case FactoryType.LandAnimalFactory:
                    return new LandAnimalFactory();
                case FactoryType.SeaAnimalFactory:
                    return new SeaAnimalFactory();
                default :
                    return null;
            }
        }

    }
         }


   class LandAnimalFactory:AnimalFactory
    {
        public override IAnimal GetAnimal(AnimalType animalType)
        {
            switch(animalType)
            {
                case AnimalType.Cat:
                    return new Cat();
                case AnimalType.Dog:
                    return new Dog();
                case AnimalType.Lion:
                    return new Lion();
                default :
                    return null;
            }
        }
    
}



class SeaAnimalFactory:AnimalFactory
    {
        public override IAnimal GetAnimal(AnimalType animalType)
        {
           switch(animalType)
           {
               case AnimalType.Octopus:
                   return new Octopus();
               case AnimalType.Whale:
                   return new Whale();
               default :
                   return null;

           }
        }
    }


I hope from this article you can learn how to implement the abstract factory design pattern in code.


No comments:

Post a Comment