Abstract Factory Pattern Using Generics

As we know through Factory Pattern, client class can choose type of instance but not factory, however Abstract Factory add up one more option to client class factory type selection

Before read further, first must go through Factory Patter, Ref Factory Pattern or search on internet will get plenty details on this topic

Let’s visualize a shop where customer can choose stuffed animal for free while ordering food and shop make stuffed animal and hand over as gift once food is finished completely

# So in this case we need two units

  • First one is food unit to make as per request
  • second one is stuffed animal unit to make as per request

# Very basic diagram for this pattern seems not complex to understand

Factory Pattern

# Fun part let’s create units (Note consider unit is factory which creates things as per request)

///Abstract Stuffed Animal
abstract class Animal
{
  public abstract void voice();
}
class Dog : Animal
{
  public override void voice()
  {
      Console.WriteLine("dog voice");
  }
}
class Cat : Animal
{
  public override void voice()
  {
      Console.WriteLine("cat voice");
  }
}

///Abstract Food
abstract class Food
{
  public abstract void Taste();
}
class Curry : Food
{
  public override void Taste()
  {
      Console.WriteLine("it is hot indian curry");
  }
}
class Vadapav : Food
{
  public override void Taste()
  {
      Console.WriteLine("it is awesome mumbai vada pav.");
  }
}

We are done with units code

# Abstract out unit by generics (WARNING !!!! Not for faint hearted)

abstract class AbstractUnit<P>
{
    public abstract P create<T>() where T : P, new();
}

class AnimalUnit<A> : AbstractUnit<A> where A : Animal
{
    public override A create<T>()
    {
        T obj = new T();
        return obj;
    }
}
class FoodUnit<F> : AbstractUnit<F> where F : Food
{
    public override F create<T>()
    {
        T obj = new T();
        return obj;
    }
}

# Now we need more intervention to select factory here it is !!!

/// Select Unit
class Unit
{
    public static P SelectUnit<P>() where P : new()
    {
        P factory = new P();
        return factory;
    }
}

# Final and the most import part customer come to shop just pick and choose things so simple, is’t it.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("##### Order Food #####");
        Unit.SelectUnit<AnimalUnit<Animal>>().create<Dog>().voice();
        Unit.SelectUnit<AnimalUnit<Animal>>().create<Cat>().voice();
        Console.WriteLine();
        Console.WriteLine("###### Choose Stuffed Animal #####");
        Unit.SelectUnit<FoodUnit<Food>>().create<Curry>().Taste();
        Unit.SelectUnit<FoodUnit<Food>>().create<Vadapav>().Taste();
        Console.WriteLine();
    }
}

# OUTPUT

##### Order Food #####
dog voice
cat voice

###### Choose Stuffed Animal #####
it is hot indian curry
it is awesome mumbai vada pav.

Press any key to continue . . .

Even I wonder about existence of such kind of shops !!! Visualization some times take you out of track…..