The factory method and abstract factory design patterns: managing object creation efficiently

Take advantage the factory and abstract factory creational design patterns to manage object creation in your applications seamlessly

Abstract factory

Abstract factory

Design patterns have evolved to solve problems often encountered in software applications. The factory method design pattern and the abstract factory design pattern are both creational patterns. Creational patterns are used to abstract object creation and hide the intricacies of how the objects are actually created. In this post, I will present a discussion on the factory method and abstract factory design patterns and their applicability.

Simple factory, factory method, and abstract factory design patterns

A simple factory is one that returns an instance of many different classes. Note that these classes may have the same base class or these may implement a common interface. While a simple factory can be called by the client using a static method and returns instances of classes that have the common base or parent, the factory method encapsulates the object creation logic inside the sub classes. The abstract factory design is used to a set of related instances without specifying the concrete classes -- it uses the factory method design pattern to create instances of classes.

The factory design pattern is a popular pattern that can be used to create objects. It is a creational pattern (GOF) and provides a standard and elegant way to create instances of classes. The factory design pattern is a creational design pattern that can be used to encapsulate creation of instances of classes. You can leverage the factory design pattern to create and manage the lifetime of an object. In this design pattern, you can create instances of classes without having to expose the creation logic -- you would typically have an interface for object creation and then let the subclasses decide which class needs to be instantiated. The factory method design pattern is used to defer the instantiation of a class to its subclasses.

You should use the abstract factory design pattern when you would like your application to be decoupled from the way instances of classes can be created. You can also use this design pattern when you would like your application to be configured to work with multiple families of classes. The abstract factory is a super factory that can be used to create other factories or factory of factories. The methods in an abstract factory pattern are factory methods and both of these design patterns are used to decouple the implementation classes using abstract types and also factories. You can implement the abstract factory pattern using factory method, prototype of the singleton pattern.

Implementing the factory method design pattern

The following code snippet illustrates how the factory method design pattern can be implemented.

public static IDbConnection GetConnection(DataProvider providerType)
{
IDbConnection iDbConnection = null;
switch (providerType)
{
case DataProvider.SqlServer:
iDbConnection = new SqlConnection();
break;
case DataProvider.OleDb:
iDbConnection = new OleDbConnection();
break;
case DataProvider.Odbc:
iDbConnection = new OdbcConnection();
break;
case DataProvider.Oracle:
iDbConnection = new OracleConnection();
break;
default:
return null;
}
return iDbConnection;
}
In essence, the factory method design pattern provides an interface to create instances but let's the subclasses decide which class should be instantiated. Here's another example of the factory method pattern -- the code snippet given below shows how you can return connection instances based on the specific database provider.

public static IDbConnection GetConnection(DataProvider providerType)
{
IDbConnection iDbConnection = null;
switch (providerType)
{
case DataProvider.SqlServer:
iDbConnection = new SqlConnection();
break;
case DataProvider.OleDb:
iDbConnection = new OleDbConnection();
break;
case DataProvider.Odbc:
iDbConnection = new OdbcConnection();
break;
case DataProvider.Oracle:
iDbConnection = new OracleConnection();
break;
default:
return null;
}
return iDbConnection;
}

Although factory and abstract factory design patterns are creational design patterns, the former uses inheritance and depends on subclasses to create instances of classes. The latter uses composition to delegate the responsibility of creating instances of classes. In essence, while the factory method design pattern creates instances of several derived classes, the abstract factory design pattern is used to create instances of several families of classes.

Implementing the abstract factory design pattern
The abstract factory combines multiple concrete factory classes (note that we aren't referring to factory methods here) that are derived from one interface. The following code snippet illustrates how the abstract factory design pattern can be implemented.

public class SQLFactory : IFactory
{
public IHelper GetHelper()
{
return new SQLHelper();
}
public IHelper GetConfigurationHelper()
{
return new SQLConfigurationHelper();
}
}

public class OracleFactory : IFactory
{
public IHelper GetHelper()
{
return new OracleHelper();
}
public IHelper GetConfigurationHelper()
{
return new OracleConfigurationHelper();
}
}
As you can see in the code snippet above, you have one interface that returns various types. There are two factory classes namely, SQLFactory and OracleFactory - both of these classes implement the IFactory interface. The GetHelper() method returns an instance of SQLHelper or OracleHelper depending on the context. Similarly, the GetConfigurationHelper() method returns an instance of SQLConfigurationHelper or OracleConfigurationHelper depending on the context. Note that both these helper classes implement the IHelper interface.

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.