Home > Patterns and Practices > Abstract Factory Pattern

Abstract Factory Pattern

Abstract Factory Pattern

 

Well, in the last installment we saw generic data-access with Factory Method pattern. At-last ADO.NET 2.0 / Whidbey has in-built support for generic data-access. Now the helper class can be written without much ado.

 

An abstract factory is a collection related of factory methods. So an abstract factory is useful “when we need to create families (or sets) of objects.”

 

ADO.NET 2.0 has introduced abstract base classes and abstract factories to create them. Abstract base classes of various classes:

 

SqlClient class

OracleClient class

Base class

SqlConnection

OracleConnection

DbConnection

SqlCommand

OracleCommand

DbCommand

SqlDataReader

OracleDataReader

DbDataReader

SqlTransaction

OracleTransaction

DbTransaction

SqlParameter

OracleParameter

DbParameter

SqlParameterCollection

OracleParameterCollection

DbParameterCollection

SqlDataAdapter

OracleDataAdapter

DbDataAdapter*

SqlCommandBuilder

OracleCommandBuilder

DbCommandBuilder

SqlConnectionStringBuilder

OracleConnectionStringBuilder

DbConnectionStringBuilder

SqlPermission

OraclePermission

DBDataPermission*

 

Abstract Factory class:

DbProviderFactory class.

 

Factory methods on it:

CreateConnection

CreateCommand

CreateCommandBuilder

CreateConnection

CreateConnectionStringBuilder

CreateDataAdapter

CreateDataSourceEnumerator

CreateParameter

CreatePermission

 

Concrete Provider factories:

SqlClientFactory

OracleClientFactory

OledbClientFactory,

 

which derive from the abstract provider factory – DbProviderFactory

 

Now how do we create a Provider factory instance:

An utility class called DbProviderFactories provides GetFactory method which returns the appropriate provider factory given the provider string (conifgured via config file).

 

So data-access code could be like this.

 

DbProviderFactory factory = DbProviderFactories.GetFactory(“<String read from config file>”)

 

DbConnection cn = factory.CreateConnection();

DbCommand command = factory.CreateCommand(cn);

command.ExecuteScalar();

 

which is independent of the provider specific coding because of the abstract base classes and abstract factory class.

 

Abstract Factory vs. Factory Method

An abstract factory is to create multiple product families whereas a factory method is to create a single product family. An abstract factory can be implemented as a collection of factory methods.

 

An abstract factory is an object creational pattern whereas a factory method is an object creational pattern.

 

A loaded statement, my understanding of it is as follows. In case of a factory method the client is seen as the concrete sub class which implements the factory method. If you need to change the product, you need to create a new concrete subclass and hence this is class creational pattern. Whereas in an abstract factory the client is usually external and it delegates the creation to the factory. This client can use a different product by changing the factory object and this can be done at run-time / configuration time. This is more confusing because an abstract factory uses Inheritance as well as object composition to achieve the goal.

 

The GoF Class Structure Diagram

 

 

That’s it for now, bye till the next one 😉

 

Advertisements
  1. No comments yet.
  1. November 19, 2010 at 11:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: