dimanche 11 février 2018

Appropriated design pattern for the payment modules c#

As i am learning through design pattern concept and also i wanted to implement the payment modules in my project using the proper design pattern. So for that i have created some sample code.

Currently i have two concrete implementation for the payment PayPal and Credit Card. But the concrete implementation will be added further on the project.

Payment Service

public interface IPaymentService
{
    void MakePayment<T>(T type) where T : class;
}

Credit Card and Pay Pal Service

public class CreditCardPayment : IPaymentService
{
    public void MakePayment<T>(T type) where T : class
    {
        var creditCardModel = (CreditCardModel)(object)type;
        //Implmentation CreditCardPayment
    }
}

class PayPalPayment : IPaymentService
{
    public void MakePayment<T>(T type) where T : class
    {
        var payPalModel = (PayPalModel)(object)type;
        //Further Implementation will goes here
    }
}

Client Code Implementation

var obj = GetPaymentOption(payType);
obj.MakePayment<PayPalModel>(payPalModel);

Get Payment Option

private static IPaymentService GetPaymentOption(PaymentType paymentType)
{
        IPaymentService paymentService = null;

        switch (paymentType)
        {
            case PaymentType.PayPalPayment:
                paymentService = new PayPalPayment();
                break;
            case PaymentType.CreditCardPayment:
                paymentService = new CreditCardPayment();
                break;
            default:
                break;
        }
        return paymentService;
}

I thought of implementing this modules using strategy design pattern but it seems that it could be done on this way also.

Is this a proper way for creating the payment modules. Is this a design pattern?

Aucun commentaire:

Enregistrer un commentaire