Strategy Design Pattern Real-Time Example – Travel

Strategy Design Pattern Real-Time Example – Travel

In this article, I am going to discuss the Strategy Design Pattern Real-Time Example in C#. Please read our previous article where we discussed the fundamental of the Strategy Design Pattern. As we already discussed the Strategy Design Pattern belongs to the category of Behavioural Design Pattern i.e. it is used to change the behavior of an object.

Strategy Design Pattern Real-Time Example – Travel

Let us understand the Strategy Design Pattern using real-time example travel. Please have a look at the following image.

On the left-hand side, you can see a house and John is staying in the house and on the right-hand side, you can see the airport and the distance from John’s house to Airport is approximately 100 KM. Suppose John wants to go to the Airport. Then what are all the different kinds of transport modes John can use? John can use Auto, or Train, or Taxi, or Bus to go Airport. So, John has four options and he can choose any one option to go Airport.

Strategy Design Pattern Real-Time Example - Travel

As per the Strategy Design Pattern, for the particular task (problem), there will be multiple solutions and from the solutions, the client will choose only one solution at runtime. So, in our example traveling is the task and john has four options to go Airport and John will choose only one option based on the cost, convenience, and time.

Implementing the Travel example:

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

Step1: Creating Strategy

Create an interface with the name ITravelStrategy.cs and then copy and paste the below code in it. The Context object uses this interface to call the algorithm defined by ConcreteStrategy classes.

namespace StrategyPatternRealTimeExample
{
    public interface ITravelStrategy
    {
        void GotoAirport();
    }
}
Step2: Creating Concrete Strategy

These are classes implementing the ITravelStrategy interface. Let’s create four concrete strategy classes as per our business requirement.

AutoTravelStrategy

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

using System;
namespace StrategyPatternRealTimeExample
{
    public class AutoTravelStrategy : ITravelStrategy
    {
        public void GotoAirport()
        {
            Console.WriteLine("Traveler is going to Airport by Auto and will be charged Rs 600");
        }
    }
}
TrainTravelStrategy

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

using System;
namespace StrategyPatternRealTimeExample
{
    public class TrainTravelStrategy : ITravelStrategy
    {
        public void GotoAirport()
        {
            Console.WriteLine("Traveler is going to Airport by Train and will be charged Rs 200");
        }
    }
}
TaxiTravelStrategy

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

using System;
namespace StrategyPatternRealTimeExample
{
    public class TaxiTravelStrategy : ITravelStrategy
    {
        public void GotoAirport()
        {
           Console.WriteLine("Traveler is going to Airport by Taxi and will be charged Rs 1000");
        }
    }
}
BusTravelStrategy

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

using System;
namespace StrategyPatternRealTimeExample
{
    public class BusTravelStrategy : ITravelStrategy
    {
        public void GotoAirport()
        {
            Console.WriteLine("Traveler is going to Airport by bus and will be charged Rs 300");
        }
    }
}
Step3: Creating context

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

namespace StrategyPatternRealTimeExample
{
   public class TravelContext
    {
        private ITravelStrategy travelStrategy;

        // The Client will set what TravelStrategy to use by 
        // calling this method at runtime
        public void SetTravelStrategy(ITravelStrategy strategy)
        {
            this.travelStrategy = strategy;
        }

        public void gotoAirport()
        {
            travelStrategy.GotoAirport();
        }
    }
}
Step4: Client code

Please modify the main method as shown below.

using System;
namespace StrategyPatternRealTimeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter Travel Type : Auto or Bus or Train or Taxi");
            string travelType = Console.ReadLine();
            Console.WriteLine("Travel type is : " + travelType);

            TravelContext ctx = null;
            ctx = new TravelContext();

            if ("Bus".Equals(travelType, StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetTravelStrategy(new BusTravelStrategy());
            }
            else if ("Train".Equals(travelType, StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetTravelStrategy(new TrainTravelStrategy());
            }
            else if ("Taxi".Equals(travelType, StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetTravelStrategy(new TaxiTravelStrategy());
            }
            else if ("Auto".Equals(travelType, StringComparison.InvariantCultureIgnoreCase))
            {
                ctx.SetTravelStrategy(new AutoTravelStrategy());
            }

            ctx.gotoAirport();
            
            Console.Read();
        }
    }
}

Output:

Strategy Design Pattern Real-time Example

In the next article, I am going to discuss the Strategy Design Pattern Real-time Example- Payment. Here, in this article, I try to explain Strategy Design Pattern Real-Time Example – Travel step by step and I hope this is one of the best examples of Strategy Design Pattern

Leave a Reply

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