Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Strategy design pattern.
The Strategy design pattern is a behavioral design pattern that allows defining a set of methods that can be replaced at runtime. This pattern allows methods with different approaches to be independent of the objects they use. By using this pattern, the behavioral of objects can be changed at runtime and based on conditions.
The Strategy pattern consists of different components:
Below is an example of how to use the Strategy design pattern in C#:
// Context class
public class ShoppingCart
{
private IShippingStrategy _shippingStrategy;
public ShoppingCart(IShippingStrategy shippingStrategy)
{
_shippingStrategy = shippingStrategy;
}
public void SetShippingStrategy(IShippingStrategy shippingStrategy)
{
_shippingStrategy = shippingStrategy;
}
public double CalculateShippingCost(Order order)
{
return _shippingStrategy.CalculateShippingCost(order);
}
}
// Strategy interface
public interface IShippingStrategy
{
double CalculateShippingCost(Order order);
}
// Concrete Strategies
public class StandardShippingStrategy : IShippingStrategy
{
public double CalculateShippingCost(Order order)
{
return 5.00;
}
}
public class ExpeditedShippingStrategy : IShippingStrategy
{
public double CalculateShippingCost(Order order)
{
return 10.00;
}
}
// Client code
var cart = new ShoppingCart(new StandardShippingStrategy());
var order = new Order();
// Calculate shipping cost using the current strategy
var shippingCost = cart.CalculateShippingCost(order); // returns 5.00
// Change the strategy to expedited shipping
cart.SetShippingStrategy(new ExpeditedShippingStrategy());
// Calculate shipping cost using the new strategy
shippingCost = cart.CalculateShippingCost(order); // returns 10.00
In this example, the strategy design pattern is used to calculate purchase shipping costs. ShoppingCart class as Context, IShippingStrategy interface as Strategy and StandardShippingStrategy and ExpeditedShippingStrategy classes are concrete strategies. Client code can dynamically change the shipping strategy by calling the SetShippingStrategy method on the ShoppingCart object. Finally, by calling CalculateShippingCost, the final value changes according to the selected strategy.
The strategy design pattern is useful when there are multiple algorithms that can be used interchangeably. By encapsulating each algorithm in a separate class and implementing them using a common interface, it provides a way to change the approach at runtime without affecting the client code.
To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.
I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.
leave a comment