top of page

Factory Method of Creational Design Patterns for .Net Core

Updated: May 18, 2023

In this article, we will address a common problem encountered in applications that receive payments from multiple banks. The number of banks involved in the payment process can change over time, creating a challenge for maintaining a flexible and easily manageable system. We will discuss a solution that utilizes a single point of decision-making to determine the bank that should process the payment based on user information. By implementing this approach, we can seamlessly add or remove banks without disrupting the existing payment processing flow.


When an application receives payments from multiple banks, deciding which bank should handle the payment based on user-entered information leads to the proliferation of complex conditions. Introducing a new bank payment system not only requires modifications to the codebase but also creates the risk of affecting the functionality of other banks within the system.


To address this problem, we propose the implementation of a single point of decision-making during the payment process. This central point will determine the appropriate bank to handle the payment based on user information, simplifying the management of banks within the application.

Creational Design Pattern

Payment Interface

The first step is to establish a common code interface that all banks will adhere to. We create an interface called "IPayment" that defines the payment method signature.

public interface IPayment
    IPayOutputModel Pay(IPayInputModel payInputModel);

All banks participating in the payment process will need to implement this "IPayment" interface.

Creating a Bank Implementation: Let's consider three banks: X, Y, and Z. We will start by creating an implementation for one of them, specifically XBank.

public class XBankPayment : IPayment
    public IPayOutputModel Pay(IPayInputModel payInputModel)
        XBankPayInputModel inputModel = (XBankPayInputModel)payInputModel;
        // Process payment logic specific to XBank
        return new YBankPayOutputModel
            BankName = typeof(XBankPayment).Name,
            TransactionId = nameof(XBankPayment) + inputModel.OrderNumber

Within the Pay method, we can implement the payment logic specific to XBank. The example code demonstrates the necessary conversion of the generic payInputModel to the specific input model for XBank. After processing the payment, a unique transaction ID is generated for XBank.

Factory Method Implementation: To consolidate the different bank implementations into a single point of management, we introduce the Factory Method design pattern. This method helps us determine which bank should process the payment based on the provided card number.

public static class PaymentFactoryMethod
    public static IPayment InitializePayment(string cardNumber)
        // Cards starting with number 1: XBank
        // Cards starting with number 2: YBank
        // Cards starting with number 3: ZBank
        IPayment payment = cardNumber switch
            string s when s.StartsWith("1") => new XBankPayment(),
            string s when s.StartsWith("2") => new YBankPayment(),
            string s when s.StartsWith("3") => new ZBankPayment(),
            _ => new XBankPayment()
        return payment;

In the InitializePayment method, we utilize a switch case statement to determine the appropriate bank implementation based on the card number provided. By using this method, we can dynamically add new payment systems by adding additional cases or remove existing ones by deleting the respective cases. This centralizes the management of bank payments, simplifying the codebase and ensuring a flexible system.


By implementing a flexible bank payment processing structure in our application, we can handle payments from multiple banks efficiently. The use of a single point of decision-making based on user information allows us to seamlessly add or remove banks without disrupting the payment flow. By adhering to a common interface and utilizing a Factory Method, we can easily manage and scale our payment system while providing a smooth experience for our users.



bottom of page