Design patterns: Strategy

Sławomir Kowalski
Sep 25, 2018 · 6 min read
Image for post
Image for post

Today we start the last of design patterns (in the end !!!), which is called Strategy at reading of this article before someone reaches the bottom of the article to the section “relations with other design patterns” will come to the conclusion that the Strategy pattern is very similar to the State pattern, however, they differ in purpose, but everything in turn :)

Discussion

The main goal of the Strategy pattern is described in the picture below:

Image for post
Image for post
  • Interface Interface that has defined methods needed for all algorithms.
  • ConcreteStrategy classes inheriting from the Interface interface that implement specific algorithms.
Image for post
Image for post

Intent

  • Defining the family of algorithms, encapsulating these algorithms and using them depending on the situation.
  • Encapsulate implementation details in derived classes.

Problem

Suppose you have to do a project to save files in various formats, png .txt .csv .jpg etc. the Strategy pattern will be perfect for this, which will separate the algorithms that write files in various formats and encapsulate these algorithms to easier maintain these algorithms in the future.

Use when:

  • You have a lot of algorithms entangled with each other performing different functions, then it is better to arrange them, separate them and encapsulate them.

Structure

As usual, the strategy pattern diagram.

Image for post
Image for post

Examples

Diagram of the Strategy pattern in code

Let’s see now what the Strategy pattern in the code looks like.

namespace StrategyPattern
{
class Context
{
private Interface _strategy;
public Context(Interface strategy)
{
_strategy = strategy;
}
public void ContextInterface()
{
_strategy.Algorithm();
}
}
}
namespace StrategyPattern
{
interface Interface
{
void Algorithm();
}
}
namespace StrategyPattern
{
class ImplementationOne : Interface
{
public void Algorithm()
{
Console.WriteLine("Called ImplementationOne");
}
}
}
namespace StrategyPattern
{
class ImplementationTwo : Interface
{
public void Algorithm()
{
Console.WriteLine("Called ImplementationTwo");
}
}
}
namespace StrategyPattern
{
class Program
{
static void Main(string[] args)
{
Context context;
context = new Context(new ImplementationOne());
context.ContextInterface();
context = new Context(new ImplementationTwo());
context.ContextInterface();
Console.ReadKey();
}
}
}
Image for post
Image for post

Real-life example

The way of transport to the airport

Let’s assume that we have to do a system in which the client will be able to decide which way go to the airport, for different ways of transport are fit different algorithms, so it will be good to use the Strategy pattern, see the picture below to show how it looks.

Image for post
Image for post
namespace TransportToTheAirport
{
class TransportToAirport
{
private StrategyOfTransport _strategy;
public TransportToAirport(StrategyOfTransport strategy)
{
_strategy = strategy;
}
public void Transport()
{
_strategy.Transport();
}
}
}
namespace TransportToTheAirport
{
abstract class StrategyOfTransport
{
public abstract void Transport();
}
}
namespace TransportToTheAirport
{
class Car : StrategyOfTransport
{
public override void Transport()
{
Console.WriteLine("Client transported by: " + GetType().Name + "\n");
}
}
}
namespace TransportToTheAirport
{
class CityBus : StrategyOfTransport
{
public override void Transport()
{
Console.WriteLine("Client transported by: "+GetType().Name+"\n");
}
}
}
namespace TransportToTheAirport
{
class Taxi : StrategyOfTransport
{
public override void Transport()
{
Console.WriteLine("Client transported by: " + GetType().Name + "\n");
}
}
}
namespace TransportToTheAirport
{
class Program
{
static void Main(string[] args)
{
TransportToAirport transporttoairport;
Console.WriteLine("Client wants to be transported by CityBus"); transporttoairport = new TransportToAirport(new CityBus());
transporttoairport.Transport();
Console.WriteLine("Client wants to be transported by Car"); transporttoairport = new TransportToAirport(new Car());
transporttoairport.Transport();
Console.WriteLine("Client wants to be transported by Taxi"); transporttoairport = new TransportToAirport(new Taxi());
transporttoairport.Transport();
Console.ReadKey();
}
}
}
Image for post
Image for post
Image for post
Image for post

Relations with other design patterns

  • Implementation of the Strategy pattern is very similar to the State pattern, these patterns differ only in the purpose of use.
  • Objects of the Strategy template often use the Flyweight pattern.
  • Patterns Strategy, State, Bridge and to some extent Adapter have similar structures, i.e. head (client or handle) and body, only differ in purpose, they are all for different problems
  • The Strategy pattern allows you to change the guts of the object and Decorator its skin.

Summary

That’s all about the Strategy🙂.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store