Overview
The purpose of this article is to show how Factory and Abstract Factory design patterns simplify the creation of complex objects in modern software development. By implementing these patterns, developers can improve code maintainability, scalability, and overall codebase navigability. This article explores detailed examples of these patterns in C# 12 within the context of .NET Core.
Understanding the Factory Pattern
A Factory Pattern encapsulates object creation by creating objects based on input parameters through a centralised factory method. Through the factory method, clients do not need to specify the exact class of the object that will be created, promoting loose coupling.
Components of the Factory Pattern
The product interface defines objects created by the factory method.
// Product interface
namespace FactoryAndAbstractFactoryDemo;
public interface IProduct
{
string Operation();
}
Implement the Product interface to define specific objects that the factory method creates.
// Concrete Product A
namespace FactoryAndAbstractFactoryDemo;
public class ConcreteProductA : IProduct
{
public string Operation()
{
return "Result of ConcreteProductA operation.";
}
}
// Concrete Product B
namespace FactoryAndAbstractFactoryDemo;
public class ConcreteProductB : IProduct
{
public string Operation()
{
return "Result of ConcreteProductB operation.";
}
}
A factory method for returning instances of a product is described as a factory method.
// Factory interface
namespace FactoryAndAbstractFactoryDemo;
public interface IFactory
{
IProduct CreateProduct();
}
// Concrete Factory A
namespace FactoryAndAbstractFactoryDemo;
public class ConcreteFactoryA : IFactory
{
public IProduct CreateProduct()
{
return new ConcreteProductA();
}
}
// Concrete Factory B
namespace FactoryAndAbstractFactoryDemo;
public class ConcreteFactoryB : IFactory
{
public IProduct CreateProduct()
{
return new ConcreteProductB();
}
}
Objects are created by the client using the factory method without knowing the concrete classes of the objects.
namespace FactoryAndAbstractFactoryDemo;
public class Client
{
private readonly IFactory _factory;
public Client(IFactory factory)
{
_factory = factory;
}
public string ClientCode()
{
var product = _factory.CreateProduct();
return product.Operation();
}
}
Implementation in C# 12
In C# 12, let's demonstrate how the Factory pattern works:
using FactoryAndAbstractFactoryDemo;
Console.WriteLine("Hello, from Ziggy Rafiq!");
// Client usage with Concrete Factory A
IFactory factoryA = new ConcreteFactoryA();
Client clientA = new Client(factoryA);
Console.WriteLine(clientA.ClientCode());
// Client usage with Concrete Factory B
IFactory factoryB = new ConcreteFactoryB();
Client clientB = new Client(factoryB);
Console.WriteLine(clientB.ClientCode());
Understanding the Abstract Factory Pattern
By providing an interface for creating families of related or dependent objects without specifying their concrete classes, the Abstract Factory Pattern extends the Factory Pattern. Switching between different product families is easy, and clients are guaranteed to use products from the same family.
Components of the Abstract Factory Pattern
A detailed description of the Abstract Factory pattern was provided in the previous response.
Implementation in C# 12
The Abstract Factory pattern in C# 12 involves defining interfaces, concrete classes, and clients that utilize these factories.
Summary
Developers can greatly enhance the maintainability and navigability of their codebases by mastering the Factory and Abstract Factory patterns in C# 12. These patterns provide powerful abstractions that simplify object creation and management, promoting code reuse and scalability. This course provides developers with essential skills for building robust and modular software solutions that adapt to evolving requirements and improve overall software quality using .NET Core examples.
Thank you for taking the time to read this article and I have uploaded the code examples to my GitHub repository. If you have enjoyed and liked this article, please follow me on LinkedIn https://www.linkedin.com/in/ziggyrafiq/ and click the like button.