Design patterns — State vs Strategy pattern with examples

Stepan Markakov
3 min readOct 23, 2020

State pattern

As wikipedia says:

The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines.

Simple example of finite-state machine is vending machine which state depends whether you put coins into vending machine or not.

Now after we got a definition of the state pattern, we are closer to a real life example of using state pattern. Let us talk about steering wheel in toy automobile. Steering wheel can be replaced. We can set bigger or smaller steering wheel. It is not a rule, however, let us think that small steering wheel makes bigger angle of automobile front wheels, than bigger steering wheel.

So, we can conclude that our automobile behaves differently dependent on the steering tool we set. For example, if we set smaller steering wheel, our automobile will turn left or right faster.

Thus, automobile responds to the events such as «TurnLeft()» or «TurnRight()». However, the angle of the automobile wheels which can be turned depending on the currently selected steering wheel. Let us try to code:

public interface ISteeringWheel{    void TurnLeft();    void Straight();    void TurnRight();}
public class BigSteeringWheel : ISteeringWheel
{ public void Straight() { Console.WriteLine("BigSteeringWheel is straight"); }
public void TurnLeft()
{ Console.WriteLine("BigSteeringWheel is turned left 10
degrees");
}

public void TurnRight()
{ Console.WriteLine("BigSteeringWheel is turned right 10
degrees");
}}public class SmallSteeringWheel : ISteeringWheel{ public void Straight() { Console.WriteLine("SmallHandleBar is straight"); }
public void TurnLeft()
{ Console.WriteLine("SmallHandleBar is turned left
20 degrees");
}
public void TurnRight() { Console.WriteLine("SmallHandleBar is turned right 20
degrees");
}
}
public class ToyAutomobile{ public ISteeringWheel SteeringWheel { get; private set; } public ToyAutomobile() { SteeringWheel = new BigSteeringWheel(); } public void TurnLeft() { SteeringWheel.TurnLeft(); }
public void TurnRight()
{ SteeringWheel.TurnRight(); }
public void SetSteeringWheel(ISteeringWheel handleBar)
{ SteeringWheel = handleBar; }
}

Strategy pattern

Definition from the Wikipedia:

The strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

Pay attention to words such as «family of algorithms to use». So let us imagine we have a real automobile and when a driver turns the steering wheel left, we want that our automobile will do the following actions:

- turn the automobile wheels left 10 degrees

- turn on the left orange signal of the automobile

So, the above two actions could be considered as «family algorithms to use».

Let us code this example:

public interface ISteeringWheel{    void TurnLeft();    void Straight();    void TurnRight();}public class BigSteeringWheel : ISteeringWheel{    public void Straight()    {        Console.WriteLine("BigSteeringWheel is straight");    } 
public void TurnLeft()
{ Console.WriteLine("BigSteeringWheel is turned left
10 degrees");
}
public void TurnRight() { Console.WriteLine("BigSteeringWheel is turned right
10 degrees");
}}public interface ITurnSignal{ void TurnOnLeft(); void TurnOnRight();}public class OrangeTurnSignal : ITurnSignal{ public void TurnOnLeft() { Console.WriteLine("Left OrangeTurnSignal is turned on"); } public void TurnOnRight() { Console.WriteLine("Right OrangeTurnSignal is turned on"); }}public class Automobile{ public ISteeringWheel SteeringWheel { get; private set; } public ITurnSignal TurnSignal { get; private set; }
public Automobile()
{ SteeringWheel = new BigSteeringWheel(); TurnSignal = new OrangeTurnSignal(); } public void TurnLeft() { SteeringWheel.TurnLeft(); TurnSignal.TurnOnLeft(); }
public void TurnRight()
{ SteeringWheel.TurnRight(); TurnSignal.TurnOnRight(); }}

SUM UP

The main difference lies in their intentions. Technically, State and Strategy pattern look very similar. The main difference lies in:

  • The State pattern changes state of the context when it is required and state can be changed many times. So context changes its state or state can set another state
  • Strategy pattern sets strategy and strategy can be changed very rarely and context does not change its strategy.

In addition, we can think of the Strategy pattern as a nice alternative to subclassing. Inheritance gives to us a very tight coupling between classes and this coupling between classes is defined at compile time. However, Strategy pattern uses composition that allows setting behavior at run time by composing with a different object.

State pattern is also can be considered as an alternative to replace many «if — else» statements in class.

--

--

Stepan Markakov
0 Followers

I am software engineer. I like to learn and like to develop software. I program with C#, SQL and JavaScript