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. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos