As wikipedia says about State pattern:
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.
Let us talk about real world example, it is a steering wheel in 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");
}
}
and Automobile
class:
public class Automobile
{
public ISteeringWheel SteeringWheel { get; private set; }
public Automobile()
{
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.
The Steering wheel algorithm:
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");
}
}
The turn signal algorithm:
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");
}
}
And the automobile class:
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();
}
}
CONCLUSION:
The State pattern
and the Strategy pattern
look very similar to each other. However, there is a tiny difference that State pattern
has a single state and all behaviors such as «TurnLeft» and «TurnRight» are encapsulated in one class. On the other hand, Strategy pattern
does not have a single state, but it has many states such as «SteeringWheel» and «TurnSignal». These different behaviors are encapsulated using different strategy objects such as «SteeringWheel» and «TurnSignal» objects. Therefore, this is a main difference between State and Strategy pattern.
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.