Sunday, June 5, 2016

Abstract Factory Pattern

Abstract Factory pattern used to create related objects. Abstract Factory pattern falls under creational pattern of GOF (Gang of Four) Pattern. Abstract Factory pattern may internally use Factory Pattern or Builder Pattern to created related or dependent objects. This is also sometime called Factory of factories or super-set of the Factory Pattern.

When to use –


When you want to create family of related or dependent objects you can go for Abstract Factory pattern.

Code –
public enum ResourceType
{
    DotNet,
    Java
}
public enum SkillType
{
    Silverlight,
    WCF,
    JDBC,
    Spring
}
public interface IResource
{
    string DoWork();
}
public interface IDotNetResource : IResource
{
    string WhoAmI();
}
public class Silverlight : IDotNetResource
{
    public string DoWork()
    {
        return "I'm writing Silverlight code";
    }

    public string WhoAmI()
    {
        return "I'm Dotnet developer";
    }
}
public class WCF : IDotNetResource
{
    public string DoWork()
    {
        return "I'm writing WCF code";
    }

    public string WhoAmI()
    {
        return "I'm Dotnet developer";
    }
}
public interface IJavaResource : IResource
{
    string WhoAmI();
}
public class JDBC : IJavaResource
{
    public string DoWork()
    {
        return "I'm writing JDBC code";
    }

    public string WhoAmI()
    {
        return "I'm Java developer";
    }
}
public class Spring : IJavaResource
{
    public string DoWork()
    {
        return "I'm writing Spring code";
    }
    public string WhoAmI()
    {
        return "I'm Java developer";
    }
}
//Dotnet Reource Factory - Creates Dotnet resource objects based on skill type
public class DotNetResourceFactory
{
    public static IDotNetResource GetResource(SkillType skillType)
    {
        switch (skillType)
        {
            case SkillType.Silverlight:
                return new Silverlight();
            case SkillType.WCF:
                return new WCF();
            default:
                throw new Exception("Not supported Dotnet skill");
        }
    }
}
//Java Reource Factory - Creates Java resource objects based on skill type
public class JavaResourceFactory
{
    public static IJavaResource GetResource(SkillType skillType)
    {
        switch (skillType)
        {
            case SkillType.JDBC:
                return new JDBC();
            case SkillType.Spring:
                return new Spring();
            default:
                throw new Exception("Not supported Java skill");
        }
    }
}
//Abstract Factory - Gets resources from different resource factories based on inputs
public static class ResourceFactory
{
    public static IResource GetResource(ResourceType resourceType, SkillType skillType)
    {
        switch (resourceType)
        {
            case ResourceType.DotNet:
                IDotNetResource dotnetResource = DotNetResourceFactory.GetResource(skillType);
                Console.WriteLine(dotnetResource.WhoAmI());
                return dotnetResource;
            case ResourceType.Java:
                IJavaResource javaResource = JavaResourceFactory.GetResource(skillType);
                Console.WriteLine(javaResource.WhoAmI());
                return javaResource;
            default:
                throw new Exception("Not supported resource");
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        IResource myResource = ResourceFactory.GetResource(ResourceType.DotNet, SkillType.Silverlight);
        Console.WriteLine(myResource.DoWork());
        myResource = ResourceFactory.GetResource(ResourceType.DotNet, SkillType.WCF);
        Console.WriteLine(myResource.DoWork());
        myResource = ResourceFactory.GetResource(ResourceType.Java, SkillType.JDBC);
        Console.WriteLine(myResource.DoWork());
        myResource = ResourceFactory.GetResource(ResourceType.Java, SkillType.Spring);
        Console.WriteLine(myResource.DoWork());
        Console.ReadLine();
    }
}


Output –











In this example, there are two factories DotnetResourceFactory and JavaResourceFactory. Both factories creates resource based on the given skills. We can add number of factories as per requirement. Resource Factory works as an abstract factory and gets resource from different factories based on user input.

Advantages –

  • Loosely coupled code.
  • Easy to extend and maintain.
  • It provides isolation since products are created in concrete Factory.


Disadvantages –

  • Adding new product may require change in factory and concrete class.


Hope this post helps you to understand Abstract Factory Pattern. Please leave your feedback in comments below.


References –

See Also –

No comments:

Post a Comment