DEV Community

Cover image for Strategy Design Patterns in C#
Askar Musthaffa
Askar Musthaffa

Posted on

Strategy Design Patterns in C#

The Strategy Design Pattern streamlines your code by encapsulating algorithms into separate classes, allowing them to be easily interchangeable. Here's a simplified implementation using a booking system.

We start by defining an interface IBookingStrategy which declares a method Book. This interface serves as a contract that concrete booking strategies must adhere to.

public interface IBookingStrategy
{
    void Book(string item);
}
Enter fullscreen mode Exit fullscreen mode

Next, create the context class, BookingContext:

public class BookingContext(IBookingStrategy _booking)
{
    public void BookItem(string item)
    {
        _booking.Book(item);
    }

    public void SetStrategy(IBookingStrategy booking)
    {
        _booking = booking;
    }
}
Enter fullscreen mode Exit fullscreen mode

The BookingContext class encapsulates the strategies. It has a private field _booking of type IBookingStrategy to hold the current booking strategy.
Its constructor takes an instance of IBookingStrategy and initializes the _booking field with it.
The BookItem method delegates the booking process to the current strategy.
The SetStrategy method allows changing the strategy dynamically at runtime.

Now, implement concrete booking strategies:

public class FlightBooking : IBookingStrategy
{
    public void Book(string item)
    {
        Console.WriteLine($"Booking flight for {item}.");
    }
}

public class HotelBooking : IBookingStrategy
{
    public void Book(string item)
    {
        Console.WriteLine($"Booking hotel for {item}.");
    }
}

public class CarRentalBooking : IBookingStrategy
{
    public void Book(string item)
    {
        Console.WriteLine($"Booking car rental for {item}.");
    }
}
Enter fullscreen mode Exit fullscreen mode

We have concrete classes like FlightBooking, HotelBooking, and CarRentalBooking, each implementing the IBookingStrategy interface. Each class provides its own implementation of the Book method, specifying how to book a flight, hotel, or car rental respectively.

Usage:

var bookingContext = new BookingContext(new FlightBooking());
bookingContext.BookItem("New York");

bookingContext.SetStrategy(new HotelBooking());
bookingContext.BookItem("London");

bookingContext.SetStrategy(new CarRentalBooking());
bookingContext.BookItem("Los Angeles");
Enter fullscreen mode Exit fullscreen mode

We create an instance of BookingContext, passing a concrete booking strategy to its constructor. Then we use the BookItem method to book various items.
We can also dynamically switch between strategies using the SetStrategy method.

Output:

Booking flight for New York.
Booking hotel for London.
Booking car rental for Los Angeles.
Enter fullscreen mode Exit fullscreen mode

In conclusion, the Strategy pattern promotes code flexibility and reusability. By encapsulating algorithms into separate classes, you can easily swap functionalities at runtime without modifying existing code. Check out the GitHub repository for the complete project. If you find this helpful, give it a ⭐️. Thanks for reading!

Top comments (0)