Abstract Factory Design Pattern C# .NET

Abstract Factory in the .NET Framework

ADO.NET 2.0 includes two new Abstract Factory classes that offer provider independent data access techniques.

They are: DbProviderFactory and DbProviderFactories. The DbProviderFactory class creates the ‘true’ (i.e. the database specific) classes you need, such as SqlClientConnection, SqlClientCommand, and SqlClientDataAdapter.

Each managed provider (such as SqlClient, OleDb, ODBC, and Oracle) has its own DbProviderFactory class. DbProviderFactory objects are created by the DbProviderFactories class, which itself is a factory class. In fact, it is a factory of factories — it manufactures different factories, one for each provider.

When Microsoft talks about Abstract Factories they mean types that expose factory methods as virtual or abstract instance functions and return an abstract class or interface.

Definition :

Provide an interface for creating families of related or dependent objects without specifying their concrete classes

Participants:

The classes and/or objects participating in this pattern are:
AbstractFactory (ManufacturerFactory)
 

declares an interface for operations that create abstract products

ConcreteFactory (AppleFactory, NokiaFactory)
 

implements the operations to create concrete product objects

AbstractProduct (3G, 2G)
 

declares an interface for a type of product object

Product (iPhone 2G/3G, N81, N95)
 

defines a product object to be created by the corresponding concrete factory implements the AbstractProduct interface

Client (PhoneWorld)
 

uses interfaces declared by AbstractFactory and AbstractProduct classes

 

 

UML Diagram:

Abstract Factory Design Patter

using System;
 
namespace Osman.Abstract
{
 
    /// <summary></summary>
    /// The 'AbstractFactory' interface.
    /// 
    interface IManufacturerFactory
    {
        I3G Create3GPhone();
        I2G Create2GPhone();
    }
 
    /// <summary></summary>
    /// The 'ConcreteFactory1' class.
    /// 
    class AppleFactory : IManufacturerFactory
    {
 
        #region IManufacturerFactory Members
 
        public I3G Create3GPhone()
        {
            return new iPhone3G();
        }
 
        public I2G Create2GPhone()
        {
            return new iPhone2G();
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// The 'ConcreteFactory2' class.
    /// 
    class NokiaFactory : IManufacturerFactory
    {
 
        #region IManufacturerFactory Members
 
        public I3G Create3GPhone()
        {
            return new N95();
        }
 
        public I2G Create2GPhone()
        {
            return new N81();
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// &quot;AbstractProductA&quot;
    /// 
    interface I2G
    {
        void Support2G();
    }
 
    /// <summary></summary>
    /// &quot;AbstractProductB&quot;
    /// 
    interface I3G
    {
        void Support3G();
    }
 
    /// <summary></summary>
    /// The &quot;ProductA1&quot; class
    /// 
    class iPhone2G : I2G
    {
        #region I2G Members
 
        public void Support2G()
        {
            // Support 2G
            Console.WriteLine(this.GetType().Name + &quot; supports 2G&quot;);
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// The &quot;ProductB1&quot; class
    /// 
    class iPhone3G : I3G
    {
 
        #region I3G Members
 
        public void Support3G()
        {
            // Support 3G
            Console.WriteLine(this.GetType().Name + &quot; supports 3G&quot;);
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// The &quot;ProductA2&quot; class
    /// 
    class N81 : I2G
    {
        #region I2G Members
 
        public void Support2G()
        {
            // Support 2G
            Console.WriteLine(this.GetType().Name + &quot; supports 2G&quot;);
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// The &quot;ProductB2&quot; class
    /// 
    class N95 : I3G
    {
 
        #region I3G Members
 
        public void Support3G()
        {
            // Support 3G
            Console.WriteLine(this.GetType().Name + &quot; supports 3G&quot;);
        }
 
        #endregion
    }
 
    /// <summary></summary>
    /// The 'Client' class (according to pattern UML).
    /// 
    class PhoneWorld
    {
        private I3G obj3GPhone;
        private I2G obj2GPhone;
 
        /// <summary></summary>
        /// Contructor of PhoneWorld
        /// 
        /// <param name="manufacturer" />Manufacturer of the phone world that is created.
        public PhoneWorld(Manufacturer manufacturer)
        {
            // Get fully qualified factory name
            string name = this.GetType().Namespace + &quot;.&quot; +
                manufacturer.ToString() + &quot;Factory&quot;;
 
            // Dynamic factory creation
            IManufacturerFactory factory =
                (IManufacturerFactory)System.Activator.CreateInstance
                (Type.GetType(name));
            obj3GPhone = factory.Create3GPhone();
            obj2GPhone = factory.Create2GPhone();
        }
 
        /// <summary></summary>
        /// Runs the SupportChain in the Phone world.
        /// 
        public void SupportChain()
        {
            obj3GPhone.Support3G();
            obj2GPhone.Support2G();
        }
    }
 
    /// <summary></summary>
    /// Enumeration of Phone Manufacturer.
    /// 
    public enum Manufacturer
    {
        /// <summary></summary>
        /// Represents Apple.
        /// 
        Apple,
 
        /// <summary></summary>
        /// Represents Nokia.
        /// 
        Nokia
    }
}

Main Class

   /// <summary></summary>
    /// Abstract Factory Design Pattern.
    /// 
    class MainEntry
    {
        /// <summary></summary>
        /// Entry point into console application.
        /// 
        public static void Main()
        {
            // Create and run the Apple phone world
            PhoneWorld objPhoneWorld = new PhoneWorld(Manufacturer.Apple);
            objPhoneWorld.SupportChain();
 
            // Create and run the Nokia phone world
            objPhoneWorld = new PhoneWorld(Manufacturer.Nokia);
            objPhoneWorld.SupportChain();
 
            // Wait for user input
            Console.Read();
        }
    }

 

Popularity: 2% [?]

Share
You can leave a response, or trackback from your own site.

Leave a Reply