Factory Method of Creational Design Patterns for .Net Core

Samet Çınar
3 min readOct 28, 2021

--

Problem:

Let’s say we have an app for which we get paid. For now, we receive payments from X, Y and Z banks on this application. The number of banks may increase or decrease in the future. We need to establish a flexible structure.

Deciding which bank should be processed according to the information entered by the user creates many conditions. When a new bank payment system arrives, it creates a big problem for others to be affected.

Solution :

During the payment process, it will be very useful to determine a single point that will decide which bank will receive the payment according to the user’s information.

Payment screen will communicate with only one point, when a new bank payment is added, it can be added from a single point and removed from a single point.

Payment instead of Product

First of all, we need to determine a code interface that all banks will use in common.

public interface IPayment{   IPayOutputModel Pay(IPayInputModel payInputModel);}

All banks will need to implement the “IPayment” interface.
The specified Input and Output must be met.
We had banks X, Y and Z. Let’s start by creating one of them.

public class XBankPayment : IPayment{public IPayOutputModel Pay(IPayInputModel payInputModel){XBankPayInputModel inputModel = (XBankPayInputModel)payInputModel;//pay..return new YBankPayOutputModel{BankName = typeof(XBankPayment).Name,TransactionId = nameof(XBankPayment) + inputModel.OrderNumber};}}

Let’s imagine that we go to the bank and withdraw the money in the section we have indicated as //pay..
We will use the returned bank name value in our test operations. Here, each bank can create a unique Transaction Id value.

Suppose we do it in Y and Z from these concrete objects. From now on, we will ensure that these bank payments are combined at one point. So we’re going to do the Factory Method.

In order for the Factory Method to understand banks, we need to set a value. We can get this value parametrically from an environment file, database, etc.

We can make reflections according to the data we receive. We will prefer switch case because reflection causes performance loss at runtime.

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 ZBankIPayment 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;}}

“PaymentFactoryMethod” contains “InitializePayment” method as static. This method decides which bank will receive the payment according to the card number entered.

Thus when a new payment system arrives, I can add the “WBankPayment” object that we created as a new case or if there is a bank that has been removed, I can delete the existing case transaction and manage bank payments from a single point.

Repo url : https://github.com/gsmtcnr/DesignPattern.Creational.FactoryMethod

Yazının Türkçe kaynağı için :
http://www.sametcinar.com/factory-method-design-pattern-net-core/

--

--

Samet Çınar
Samet Çınar

No responses yet