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

For updated version of the article please visit www.agnosticdevs.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>

Published Wednesday, September 26, 2012 10:44 PM by nijhawan.saurabh

Comments

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

Thursday, September 27, 2012 12:12 PM by Bob

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

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

Friday, September 28, 2012 5:48 AM by buy persuasive essay

Thanks for your interesting post. It is very ustful indeed. <a href="www.onlinessays.com/.../">buy persuasive essay

</a>

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

Wednesday, October 03, 2012 3:13 AM by nijhawan.saurabh

Hi Bob,

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

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

Saturday, October 06, 2012 5:51 AM by ultnnsagVjlcnYNcme

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

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

Saturday, October 06, 2012 3:10 PM by http://www.amoffice2010.com

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.

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

Friday, October 19, 2012 3:06 PM by bookmarking submission

9V8VZI Thanks for sharing, this is a fantastic blog article.Really looking forward to read more. Much obliged.

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

Friday, October 26, 2012 12:06 AM by gsnfgwoia@gmail.com

Enjoy stands out as the dynamic challenge for ones living and expansion of truley what everyone real love.

nike tn nikepascherfr.blogspot.com

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

Thursday, February 28, 2013 12:41 PM by sepmfirASayj

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

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

Tuesday, March 19, 2013 6:07 PM by Gregg

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

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

Wednesday, March 20, 2013 9:44 PM by Keefer

I like the helpful information you provide on your articles.

I will bookmark your blog and take a look at again here regularly.

I'm reasonably certain I'll be told many new stuff proper right here!

Good luck for the next!

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

Sunday, March 24, 2013 8:08 PM by Dagostino

Hi there, I enjoy reading all of your post.

I wanted to write a little comment to support you.

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

Monday, March 25, 2013 6:14 AM by Knowlton

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?

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

Sunday, March 31, 2013 3:35 PM by Minnick

Hi colleagues, how is everything, and what you desire to

say regarding this paragraph, in my view its

genuinely awesome designed for me.

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

Monday, April 15, 2013 3:52 PM by Bechtel

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!!

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

Friday, April 19, 2013 1:08 PM by Bonner

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

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

Friday, April 19, 2013 8:57 PM by Crespo

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

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

Monday, April 22, 2013 2:09 PM by Bernstein

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.

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

Monday, May 20, 2013 12:55 PM by Stevenson

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.

Leave a Comment

(required) 
(required) 
(optional)
(required)