SIGN UP MEMBER LOGIN:    
ARTICLE

Abstract Factory Design Pattern in ADO.NET 2.0

Posted by Muhammad Mosa Articles | Design & Architecture February 17, 2006
This article will discuss the Abstract Factory Design Pattern and its real world applications in .Net Framework 2.0.
Reader Level:

Introduction:

This article will discuss the Abstract Factory Design Pattern and its real world applications in .Net Framework 2.0.

About Abstract Factory:

Abstract Factory Pattern can be used when we want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several groups of classes.

Definition of Abstract Factory Pattern:

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

Structure of the Abstract Factory Pattern:



Abstract Factory Applications From .Net 2.0:

Abstract Factory is widely used in may application. Here we will see how Abstract Factory is used in ADO.NET 2.0.

ADO.NET 2.0 defines new set of abstract products classes such as DbConnection, DbCommand, DbParameter etc.... In the same time, each product has a concrete product class, like SqlConnection and OralceConnection etc... The XxxConnection classes inherit and implement DbConnection.

Also ADO.NET 2.0 brings new set of abstract factory classes to help us to implement Provider-Independent Data Access Layer. Through the use of DbProviderFactory Class which is an abstract factory class. The abstract factory -DbProviderFactory-declares set of CreateXxx Methods, like, CreateConnection, CreateCommand, CreateParamenter etc...

Each of these methods return a specific abstract product, for example CreateConnection returns DbConnection, CreateCommnad return DbCommand etc...

DbProviderFactory has set of concrete factory classes such as SqlClientFactory, OracleClientFactory etc...These concrete factories implements the CreateXxx methods to return specific products -provider specific classes.

Now have a look at the following UML Class Diagram and compare it with the Abstract Factory structure diagram:

Abstract Factory Participants:

  • AbstractFactory  (DbProviderFactory):
    Role: Declares an interface for operations that create abstract product objects.

  • ConcreteFactory (SqlClientFactory, OracleClientFactory, OleDbFactory, OdbcFactory, Other Third-Party Factory):
    Role: Implements the operations declared in the AbstractFactory to create concrete product objects.

  • AbstractProduct (DbConnection, DbCommand, DbParameter etc...):
    Role: Declares an interface for a type of product object.

  • ConcreteProduct -Product- (SqlConnection, OracleConnection, XxxConnection, SqlCommand, XxxCommand...):
    Role: Defines a product object to be created by the corresponding concrete factory and implements the AbstractProduct interface.

  • Client:
    Role: Uses only interfaces declared by AbstractFactory and AbstractProduct classes.

How Abstract Factory participants collaborate:

Normally a single instance of a ConcreteFactory class is created at run-time -singleton-. This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory. Also we should note that AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

Advantages of Abstract Factory:

  • Isolation of concrete classes:
    The Abstract Factory pattern helps you control the classes of objects that an application creates. Because a factory encapsulates the responsibility and the process of creating product objects, it isolates clients from implementation classes. Clients manipulate instances through their abstract interfaces. Product class names are isolated in the implementation of the concrete factory; they do not appear in client code.
  • Exchanging Product Families easily:
    The class of a concrete factory appears only once in an application, that is where it's instantiated. This makes it easy to change the concrete factory an application uses. It can use different product configurations simply by changing the concrete factory. Because an abstract factory creates a complete family of products, the whole product family changes at once.
  • Promoting consistency among products:
    When product objects in a family are designed to work together, it's important that an application use objects from only one family at a time. AbstractFactory makes this easy to enforce.

Disadvantages of Abstract Factory:

  • Difficult to support new kind of products:
    Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses. But you can overcome this problem by using reflection in .Net Framework and defining CreateProduct method in the AbstractFactory class.

Conclusion:

Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software would create a concrete implementation of the abstract factory and then use the generic interfaces to create the concrete objects that are part of the theme.
In our next article, we will talk about simpler Factory Patter, the Simple Factory, and we will see how t use it to create a specific concrete factory.

References:

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Mosa,

Have a doubt about your comment in the disadvantage mentioned, "But you can overcome this problem by using reflection in .Net Framework and defining CreateProduct method in the AbstractFactory class. "

Can u please explain what exactly u meant and send me an updated sample on this.

Also would like to know what exactly is the difference between "Abstract factory" and "Factory method"

Cheers,
Sunil.

Posted by sunil Jun 01, 2009

On your topic of using reflection to essentialy override or even intercept the create behavior you can always use the new keyword as well to provide a new concreate behavior. That option is also always avaliable for any method or property not marked virtual

Posted by Will Fawthrop Mar 17, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor