Difference between Factory Method and Abstract Factory design patterns using C#.Net

I have moved this blog to http://pragmaticdevs.wordpress.com/

First of all I'll just put both these patterns in context and describe their intent as in the GOF book:

 

Factory Method:

Define an interface for creating an object, but let subclasses decide which class

to instantiate. Factory Method lets a class defer instantiation to subclasses.

 

Abstract Factory:

Provide an interface for creating families of related or dependent objects without

specifying their concrete classes.

 

Points to note:  

  • Abstract factory pattern adds a layer of abstraction to the factory method pattern. The type of factory is not known to the client at compile time, this information is passed to the client at runtime (How it is passed is again dependent on the system, you may store this information in configuration files and the client can read it on execution).
  • While implementing Abstract factory pattern, the factory classes can have multiple factory methods.
  • In Abstract factory, a factory is capable of creating more than one type of product (Similar products are grouped together in a factory)

 

Sample implementation of factory method pattern

 

Let's see the class diagram first:           

 class diagram

 

ProductFactory.cs

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ProductFactory.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public abstract class ProductFactory

   18     {

   19         /// <summary>

   20         /// </summary>

   21         /// <returns>

   22         /// </returns>

   23         public abstract Product CreateProductInstance();

   24     }

   25 }

   26 

 

 

ProductAFactory.cs

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ProductAFactory.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public class ProductAFactory:ProductFactory

   18     {

   19         public override Product CreateProductInstance()

   20         {

   21             return new ProductA();

   22         }

   23     }

   24 }

   25 

 

 

 ProductBFactory.cs

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ProductBFactory.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public class ProductBFactory:ProductFactory

   18     {

   19         public override Product CreateProductInstance()

   20         {

   21             return new ProductB();

   22 

   23         }

   24     }

   25 }

   26 

 

Product.cs

    1 // -----------------------------------------------------------------------

    2 // <copyright file="Product.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public abstract class Product

   18     {

   19         public abstract string Name { get; set; }

   20     }

   21 }

   22 

 

 ProductB.cs

 

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ProductB.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public class ProductB:Product

   18     {

   19          public ProductB()

   20         {

   21 

   22             Name = "ProductB";

   23         }

   24         public override string Name { get; set; }

   25     }

   26 }

   27 

 

 

 ProductA.cs

 

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ProductA.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public class ProductA:Product

   18     {

   19         public ProductA()

   20         {

   21 

   22             Name = "ProductA";

   23         }

   24 

   25         public override string Name { get; set; }

   26     }

   27 }

   28 

 

Sample implementation of Abstract Factory pattern

 

Here's the class diagram:

 

 class diagram

As said earlier, the Abstract factory pattern adds another layer of abstraction to the factory method pattern so the Product/ProductFactory classes that we used earlier in Factory Method pattern implementation would be reused here.

 

    1 // -----------------------------------------------------------------------

    2 // <copyright file="ApplicationCaller.cs" company="">

    3 // TODO: Update copyright text.

    4 // </copyright>

    5 // -----------------------------------------------------------------------

    6 

    7 namespace FactoryMethod

    8 {

    9     using System;

   10     using System.Collections.Generic;

   11     using System.Linq;

   12     using System.Text;

   13 

   14     /// <summary>

   15     /// TODO: Update summary.

   16     /// </summary>

   17     public class ApplicationCaller

   18     {

   19         public Product PassFactory(ProductFactory pf)

   20         {

   21 

   22           Product product = pf.CreateProductInstance();

   23             return product;

   24         }

   25     }

   26 }

   27 

 

 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace FactoryMethod

    7 {

    8     using System.Configuration;

    9     using System.Net.Mime;

   10 

   11     class Program

   12     {

   13         static ProductFactory CreateSpecificFactory() 

   14         {

   15 

   16             string facType = System.Configuration.ConfigurationSettings.AppSettings["FactoryName"];

   17             if (facType == "ProductAFactory")

   18             {

   19                 return new ProductAFactory();

   20             }

   21             else

   22             {

   23                 return new ProductBFactory();

   24             }

   25         }

   26 

   27         static void Main(string[] args)

   28         {

   29             Console.WriteLine(new ApplicationCaller().PassFactory(CreateSpecificFactory()).Name);

   30 

   31         }

   32     }

   33 }

   34 

Add finally the type of factory to be used would be decided at runtime, in this sample we are reading this information from a config file and passing it to the application.

    1 <?xml version="1.0" encoding="utf-8" ?>

    2 <configuration>

    3   <appSettings>

    4     <add key="FactoryName" value="ProductBFactory"/>

    5   </appSettings>

    6 </configuration>

20 Comments

  • Nice!! Can you please upload the code for the sample. It would be helpful.

  • Hi Bob,
    I have updated the source code with code highlighting. I hope it's easy to understand now!

  • nYWsil I really like and appreciate your post.Thanks Again. Will read on...

  • By employing Windows Anytime Upgrade, you can upgrade into a moreadvanced edition of Windows 7 in your own home Premium to Ultimate or maybe Windows 7 Home Advanced to Professional Upgrade Keyor Basic to Home Premium within TEN mins.Doing that, you get to exploit extra attributes while maintaining your overall applications, files, as very well as adjustments intact.All of the progress you don't have any kind of hard drives, any delays and you also have no reason to leave your home.With all the key you can primary open Windows Anytime Upgrade by just clicking the Start press button, inputting Windows Anytime Upgrade from the search box, and then clicking Windows Anytime Upgrade from the list of results.Anyone incorporate the use of Windows Anytime Upgrade to upgrade at a 32-bit edition of Windows 7 into a 32-bit edition of Windows 7 and at a 64-bit edition of Windows 7 into a 64-bit edition of Microsoft windows 7, but you can't upgrade at a 32- bit edition of Windows 7 into a 64-bit version of Microsoft windows 7 or vice versa.

  • QIr6xn I really liked your blog article.Much thanks again.

  • I read this article fully on the topic of the comparison of latest and preceding technologies, it's amazing article.

  • Hi there, I enjoy reading all of your post.

    I wanted to write a little comment to support you.

  • Write more, thats all I have to say. Literally, it seems as though you relied on the video to
    make your point. You definitely know what youre talking
    about, why waste your intelligence on just posting videos to your weblog when you could be giving
    us something informative to read?

  • Hi colleagues, how is everything, and what you desire to
    say regarding this paragraph, in my view its
    genuinely awesome designed for me.

  • Hello, i think that i saw you visited my blog thus i came to “return the favor”.
    I'm attempting to find things to improve my site!I suppose its ok to use some of your ideas!!

  • What's up Dear, are you genuinely visiting this web page daily, if so then you will absolutely get nice knowledge.

  • Great beat ! I would like to apprentice while you amend your website, how could i subscribe for
    a blog site? The account aided me a acceptable deal.

    I had been tiny bit acquainted of this your broadcast provided bright clear
    idea

  • If some one needs to be updated with newest technologies after that he must be visit this
    web site and be up to date every day.

  • I am not sure where you are getting your information, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for fantastic information I was looking for this information for my mission.

  • tTa6wy Enjoyed every bit of your blog post.Really looking forward to read more. Really Great.

  • hYcscQ Very informative post.Much thanks again. Fantastic.

  • Love the bolts of fabric as a colagle of pattered color. I owuld want to wear all of then together!The flowing soft fabrics look perfect for the summer weather! Great to get a hit of Italy. Warms my heart!Debra

  • i got it
    Thank you

  • Hi, i believe that i saw you visited my website
    thus i got here to return the want?.I am trying to in finding issues to enhance my website!I guess its good enough to use a
    few of your ideas!!

  • chTemi This is one awesome blog post.Really looking forward to read more. Want more.

Comments have been disabled for this content.