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:
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> /// "AbstractProductA" /// interface I2G { void Support2G(); } /// <summary></summary> /// "AbstractProductB" /// interface I3G { void Support3G(); } /// <summary></summary> /// The "ProductA1" class /// class iPhone2G : I2G { #region I2G Members public void Support2G() { // Support 2G Console.WriteLine(this.GetType().Name + " supports 2G"); } #endregion } /// <summary></summary> /// The "ProductB1" class /// class iPhone3G : I3G { #region I3G Members public void Support3G() { // Support 3G Console.WriteLine(this.GetType().Name + " supports 3G"); } #endregion } /// <summary></summary> /// The "ProductA2" class /// class N81 : I2G { #region I2G Members public void Support2G() { // Support 2G Console.WriteLine(this.GetType().Name + " supports 2G"); } #endregion } /// <summary></summary> /// The "ProductB2" class /// class N95 : I3G { #region I3G Members public void Support3G() { // Support 3G Console.WriteLine(this.GetType().Name + " supports 3G"); } #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 + "." + manufacturer.ToString() + "Factory"; // 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% [?]
March 8th, 2009
admin 
Posted in
Tags: