joydip_kanjilal
Contributor

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

opinion
Jun 05, 20154 mins
Software Development

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

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.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author