Strategy Pattern Real-Time Example – Payment

Strategy Pattern Real-Time Example – Payment

In this article, I am going to discuss the Strategy Pattern Real-Time Example – Payment using C#. Please read our previous article where we discussed the Strategy Design Pattern in C# with examples.

Strategy Pattern Real-Time Example – Payment

Let us understand the Real-time example of Strategy Design Pattern using C#. Please have a look at the following image. As shown in the following image, Steve goes to a shopping mall and purchases one LED TV and one washing machine and that cost are around 90000 rupees. After purchasing the LED TV and Washing Machines, Steve goes to the bill counter and he wants to pay the money. There are three different options to pay the money. The options are Credit card, Debit Card and Cash. So, from these three options, he has to choose one option and pay the money at the bill counter.

Strategy Pattern Real-Time Example - Payment

So, as per the Strategy Design Pattern, for a particular task (problem), there will be multiple solutions and from the solutions, the user has to choose only one solution at runtime. So, in this example paying money is the task and Steve three options (Credit, Debit, and Cash) to pay the money and Steve will choose only one option at the time of billing.

Implementing the Payment example using the Strategy Design Pattern:

Let us implement the above example step by step in C# using the Strategy Pattern. This is one of the best real-time examples of the Strategy Pattern.

Step1: Creating Strategy

Create an interface with the name IPaymentStrategy.cs and then copy and paste the following code in it. The Context object is going to use this interface to call the algorithm which is defined by a ConcreteStrategy class.

namespace StrategyDesignPatternExample
{
   public interface IPaymentStrategy
    {
        void Pay(double amount);
    }
}
Step2: Creating Concrete Strategy

These are going to be the classes implementing the IPaymentStrategy interface. Let’s create three concrete strategy classes as per our business requirement.

CreditCardPaymentStrategy

Create a class file with the name CreditCardPaymentStrategy.cs and then copy and paste the following code in it.

using System;
namespace StrategyDesignPatternExample
{
    public class CreditCardPaymentStrategy : IPaymentStrategy
    {
        public void Pay(double amount)
        {
            Console.WriteLine("Customer pays Rs " + amount + " using Credit Card");
        }
    }
}
DebitCardPaymentStrategy

Create a class file with the name DebitCardPaymentStrategy.cs and then copy and paste the following code in it.

using System;
namespace StrategyDesignPatternExample
{
    public class DebitCardPaymentStrategy : IPaymentStrategy
    {
        public void Pay(double amount)
        {
            Console.WriteLine("Customer pays Rs " + amount + " using Debit Card");
        }
    }
}
CashPaymentStrategy

Create a class file with the name CashPaymentStrategy.cs and then copy and paste the following code in it.

using System;
namespace StrategyDesignPatternExample
{
    public class CashPaymentStrategy : IPaymentStrategy
    {
        public void Pay(double amount)
        {
            Console.WriteLine("Customer pays Rs " + amount + " By Cash");
        }
    }
}
Step3: Creating context

Create a class file with the name PaymentContext.cs and then copy and paste the following code in it. The PaymentContext class has a property to hold the reference of a concrete Strategy object. This property is going to be set by the user at run-time.

namespace StrategyDesignPatternExample
{
   public class PaymentContext
    {
        private IPaymentStrategy PaymentStrategy;

        // The user will set the PaymentStrategy by 
        // calling this method at runtime
        public void SetPaymentStrategy(IPaymentStrategy strategy)
        {
            this.PaymentStrategy = strategy;
        }

        public void Pay(double amount)
        {
            PaymentStrategy.Pay(amount);
        }
    }
}
Step4: Client code

Please modify the main method as shown below.

using System;
namespace StrategyDesignPatternExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Select Payment Type : CreditCard or DebitCard or Cash");            
            string PaymentType = Console.ReadLine();
            Console.WriteLine("Payment type is : " + PaymentType);

            Console.WriteLine("\nPlease enter Amount to Pay : ");
            double Amount = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Amount is : " + Amount);

            PaymentContext context = new PaymentContext();
            
            if ("CreditCard".Equals(PaymentType, StringComparison.InvariantCultureIgnoreCase))
            {
                context.SetPaymentStrategy(new CreditCardPaymentStrategy());
            }
            else if ("DebitCard".Equals(PaymentType, StringComparison.InvariantCultureIgnoreCase))
            {
                context.SetPaymentStrategy(new DebitCardPaymentStrategy());
            }
            else if ("Cash".Equals(PaymentType, StringComparison.InvariantCultureIgnoreCase))
            {
                context.SetPaymentStrategy(new CashPaymentStrategy());
            }

            context.Pay(Amount);
            Console.ReadKey();
        }
    }
}

Output:

Strategy Pattern Real-Time Example - Payment

In the next article, I am going to discuss the Interpreter Design Pattern in C# with Real-time Examples. Here, in this article, I try to explain Strategy Pattern Real-Time Example – Payment step by step and I hope now you understood the need and use of Strategy Design Pattern.

1 thought on “Strategy Pattern Real-Time Example – Payment”

Leave a Reply

Your email address will not be published. Required fields are marked *