According to Gang of Four, abstract factory patterns can be assumed as the factory for creating factories.
Learning Objectives
What is the abstract factory design pattern?
How to write code using the abstract factory design pattern?
How to create a factory provider?
How to create a client application(from the Main method) that uses factory provider
Prerequisites
Abstract factory pattern is purely an extension factory method; it’s recommended to go through the factory method before understanding abstract factory design. Design Pattern - Factory Method
Basic knowledge of OOPS concepts.
Any programming language knowledge.
The article demonstrates the abstract factory design pattern using the C# programming language. So, to begin with, C#
Getting Started
Let’s consider the same example of any Bank with account types as Savings and Current accounts. Now let’s implement the above example using the abstract factory design pattern.
Firstly, implement ISavingAccount and ICurrentAccount interfaces as follows:
public interface ISavingAccount{ }
public interface ICurrentAccount{ }
Inherit the interface in classes below
public class CurrentAccount : ICurrentAccount
{
public CurrentAccount(string message)
{
Console.WriteLine(message);
}
}
public class SavingsAccount : ISavingAccount
{
public SavingsAccount( string message)
{
Console.WriteLine(message);
}
}
Let's write an abstract class with abstract methods for each account type.
public abstract class AccountTypeFactory
{
public abstract ISavingAccount SavingAccountFactory(string message);
public abstract ICurrentAccount CurrentAccountFactory(string message);
}
Now, let's create a factory implementation named “Bank1Factory,” which provides the implementation of abstract methods.
public class Bank1Factory : AccountTypeFactory
{
public override ICurrentAccount CurrentAccountFactory(string message)
{
return new CurrentAccount(message);
}
public override ISavingAccount SavingAccountFactory(string message)
{
return new SavingsAccount(message);
}
}
The abstract factory design pattern differs from the factory method that it needs to implement a factory provider, which returns factories as per definition.
Now that we have all the abstractions and factories created. Let us design the factory provider. Please find below the code snippet for factory provider, where a static method will create a factory based upon account name.
public class AccountFactoryProvider
{
public static AccountTypeFactory GetAccountTypeFactory(string accountName)
{
if (accountName.Contains("B1")) { return new Bank1Factory(); }
else return null;
}
}
How to use abstract factory provider?
Let's take an example of a list of account numbers where if an account name consists of “B1” literal, then it will use Bank1Factory instance returned via factory provider.
static void Main(string[] args)
{
List<string> accNames = new List<string> { "B1-456", "B1-987", "B2-222" };
for (int i = 0; i < accNames.Count; i++)
{
AccountTypeFactory anAbstractFactory = AccountFactoryProvider.GetAccountTypeFactory(accNames[i]);
if (anAbstractFactory == null)
{
Console.WriteLine("Invalid " + (accNames[i]));
}
else
{
ISavingAccount savingAccount = anAbstractFactory.SavingAccountFactory("Hello saving");
ICurrentAccount currentAccount = anAbstractFactory.CurrentAccountFactory("Hello Current");
}
}
Console.ReadLine();
}
If the account name does not contain “B1” literal, then the program will output invalid {{accountName}}
Output
Please find below the output from the above code snippet.
Hello saving B1-456
Hello Current B1-456
Hello saving B1-987
Hello Current B1-987
Github Repo
ssukhpinder/AbstractFactoryPattern
Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.
Follow me on C# Publication, LinkedIn, Instagram, Twitter, Dev.to, Pinterest, Substack, Wix.