Wednesday, February 15, 2012

Abstract Factory Pattern Implementation

Abstract factory expands on the basic factory pattern. Abstract factory helps us to unite similar factory pattern classes in to one unified interface. So basically all the common factory patterns now inherit from a common abstract factory class which unifies them in a common class. All other things related to factory pattern remain same as discussed in the previous question.

A factory class helps us to centralize the creation of classes and types. Abstract factory helps us to bring uniformity between related factory patterns which leads more simplified interface for the client.


Figure: - Abstract factory unifies related factory patterns

Now that we know the basic lets try to understand the details of how abstract factory patterns are actually implemented. As said previously we have the factory pattern classes (factory1 and factory2) tied up to a common abstract factory (AbstractFactory Interface) via inheritance. Factory classes stand on the top of concrete classes which are again derived from common interface. For instance in figure ‘Implementation of abstract factory’ both the concrete classes ‘product1’ and ‘product2’ inherits from one interface i.e. ‘common’. The client who wants to use the concrete class will only interact with the abstract factory and the common interface from which the concrete classes inherit.


Figure: - Implementation of abstract factory


Now let’s have a look at how we can practically implement abstract factory in actual code. We have scenario where we have UI creational activities for textboxes and buttons through their own centralized factory classes ‘ClsFactoryButton’ and ‘ClsFactoryText’. Both these classes inherit from common interface ‘InterfaceRender’. Both the factories ‘ClsFactoryButton’ and ‘ClsFactoryText’ inherits from the common factory ‘ClsAbstractFactory’. Figure ‘Example for AbstractFactory’ shows how these classes are arranged and the client code for the same. One of the important points to be noted about the client code is that it does not interact with the concrete classes. For object creation it uses the abstract factory ( ClsAbstractFactory ) and for calling the concrete class implementation it calls the methods via the interface ‘InterfaceRender’. So the ‘ClsAbstractFactory’ class provides a common interface for both factories ‘ClsFactoryButton’ and ‘ClsFactoryText’.


Figure: - Example for abstract factory



We will just run through the sample code for abstract factory. Below code snippet ‘Abstract factory and factory code snippet’ shows how the factory pattern classes inherit from abstract factory.




Figure: - Abstract factory and factory code snippet

Figure ‘Common Interface for concrete classes’ how the concrete classes inherits from a common interface ‘InterFaceRender’ which enforces the method ‘render’ in all the concrete classes.


Figure: - Common interface for concrete classes

The final thing is the client code which uses the interface ‘InterfaceRender’ and abstract factory ‘ClsAbstractFactory’ to call and create the objects. One of the important points about the code is that it is completely isolated from the concrete classes. Due to this any changes in concrete classes like adding and removing concrete classes does not need client level changes.


Figure: - Client, interface and abstract factory

Another Example:
This pattern is essential for learning the Factory Method and Bridge patterns.

Challenge

You are working on an ORM (Object Relational Mapping) framework creation. The framework could be used in different databases like:

  • Sql Server
  • Oracle
  • MS Access
  • OleDb

Based on the database type, you need to create related classes like:\

  • SqlConnection, SqlCommand when Sql server
  • OracleConnection, OracleCommand when Oracle etc.

How to create a family of related objects without implementation of them?

Definition

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

Implementation

Our idea is to create an interface which contains all related objects like:

public abstract class DbProviderFactory
{
    public abstract DbConnection CreateConnection();
    public abstract DbCommand CreateCommand();
}


Luckily, ADO.NET already includes the same interface as Abstract Factory. The The definition is given below: (I have excluded some irrelevant methods from it)

namespace System.Data.Common
{
    public abstract class DbProviderFactory
    {
        public virtual DbCommand CreateCommand();
        public virtual DbCommandBuilder CreateCommandBuilder();
        public virtual DbConnection CreateConnection();
        public virtual DbDataAdapter CreateDataAdapter();
        public virtual DbParameter CreateParameter();
    }
}

From the above code we can see that the related classes are includes as a family without going to the implementation details.

The abstract class DbProviderFactory contains related classes which are abstract: DbCommand, DbConnection etc.

Concrete Implementations

ADO.NET provides the following implementations of the above abstract class DbProviderFactory.

  • System.Data.SqlClient.SqlClientFactory
  • System.Data.OleDb.OleDbFactory

The above classes implements all abstract methods from the base class. Thus it will be providing concrete implementations of DbConnection which is SqlConnection, DbCommand which is SqlCommand etc.

Instantiating from the above classes, we can start using the concrete implementations like following code.

SqlClientFactory factory = SqlClientFactory.Instance;

DbConnection connection = factory.CreateConnection();
DbCommand command = factory.CreateCommand();

command.Connection = connection;
command.CommandText = "query here";
command.ExecuteNonQuery();

FactoryPtrn.gif

Summary

In this article we have seen the usage of Abstract Factory pattern through inbuilt ADO.NET DbProviderFactory class.

Reference: 

No comments:

Post a Comment