Difference between Factory Method and Abstract Factory design patterns using C#.Net
- by nijhawan.saurabh
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 (Simpilar products are grouped together in
a factory)
Sample implementation of factory method pattern
Let's see the class diagram first:
ProductFactory.cs
//
-----------------------------------------------------------------------
// <copyright
file="ProductFactory.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public abstract class ProductFactory
{
/// <summary>
/// </summary>
/// <returns>
/// </returns>
public abstract Product CreateProductInstance();
}
}
ProductAFactory.cs
//
-----------------------------------------------------------------------
// <copyright
file="ProductAFactory.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class ProductAFactory:ProductFactory
{
public override Product CreateProductInstance()
{
return new ProductA();
}
}
}
//
-----------------------------------------------------------------------
// <copyright
file="ProductBFactory.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class ProductBFactory:ProductFactory
{
public override Product CreateProductInstance()
{
return new ProductB();
}
}
}
//
-----------------------------------------------------------------------
// <copyright
file="Product.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public abstract class Product
{
public abstract string Name { get; set; }
}
}
//
-----------------------------------------------------------------------
// <copyright
file="ProductA.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class ProductA:Product
{
public ProductA()
{
Name = "ProductA";
}
public override string Name { get; set; }
}
}
//
-----------------------------------------------------------------------
// <copyright
file="ProductB.cs" company="">
// TODO: Update copyright text.
// </copyright>
//
-----------------------------------------------------------------------
namespace FactoryMethod
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class ProductB:Product
{
public ProductB()
{
Name = "ProductA";
}
public override string Name { get; set; }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FactoryMethod
{
class Program
{
static void Main(string[] args)
{
ProductFactory pf = new
ProductAFactory();
Product product = pf.CreateProductInstance();
Console.WriteLine(product.Name);
}
}
}
Normal
0
false
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}