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

Posted by nijhawan.saurabh on ASP.net Weblogs See other posts from ASP.net Weblogs or by nijhawan.saurabh
Published on Thu, 27 Sep 2012 09:44:00 GMT Indexed on 2012/09/27 15:38 UTC
Read the original article Hit count: 343

Filed under:
|
|
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:           

 Class diagram

    

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);

        }

    }

}

 

 

 


© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about c#