Sometimes, we need to carry out several computations / algorithms depending on certain conditions. We go ahead in implementing those generally by applying either
switch or ternary operator or
if else. Though initially somehow we manage to write those programs, but if the program demands are too complex then it is difficult to frame such as well as to maintain.
Moreover, writing all the logic in a single place is not at all advisable as it yields to tight coupling.
In many situations, we come across the need to write computational logic/algorithms which we generally accomplish by using
if else /
switch or ternary operator. It becomes difficult at times to write such a program and later on adds a lot of cost during maintenance.
Strategy design pattern. It comes under the category of Behavioral Patterns.
- Decouples the client and the algorithm/ computation logic in separate classes
- Helps to switch algorithms at any time
- Easily allows to plug in a new algorithm
The strategy pattern comprises the following components:
- Strategy Interface - Interface common to all concrete strategies
- Concrete Strategies / Different algorithm classes - Various concrete classes that implement the strategy interface for the sake of algorithm implementation specific to itself
- Context Class - Delegates requests to the indicated Concrete Strategies received from the client. It does so as it keeps reference of the concrete strategies.
- Client - In this case, the Windows form
Let us take the example of MSWord application. The various Font Styles (e.g. Bold, Italic, etc.) can be implemented by using this design pattern. Let us see the same as under.
Implementation of Strategy Interface
IFontStyle interface looks as below:
public interface IFontStyle
public Font GetNewFontStyle(Control c);
Implementation of Concrete Strategies
There are four concrete strategy classes, viz Bold.cs, Italic.cs, Underline.cs and StrikeThru.cs.
Since all follows the same pattern in this example, so for the sake of simplicity I am describing only for the
Bold concrete class.
It goes as below:
public class Bold : IFontStyle
#region IFontStyle Members
public Font GetNewFontStyle(System.Windows.Forms.Control c)
return new Font(c.Font, FontStyle.Bold);
The code is self explanatory. The
Bold class implemented the strategy interface
IFontStyle where it is returning the font style as
Implementation of Context
FontStyleContext class is as under:
public class FontStyleContext
public FontStyleContext(IFontStyle IFontStyle)
this._IFontStyle = IFontStyle;
public Font GetFontStyle(Control c)
Implementation of Client
The client design is as under:
And the code behind for the
Bold Button click is as under:
private void btnBold_Click(object sender, EventArgs e)
objFontStyleContext = new FontStyleContext(new Bold());
richTextArea.Font = objFontStyleContext.GetFontStyle(richTextArea);
First, we have created an instance of the
FontStyleContext class and then instantiated the same with the appropriate
StrategyConcrete class. Next by calling the
GetFontStyle method, we were able to set the appropriate
Font style for the
The other buttons will have the same implementation with only call to appropriate concrete classes.
Strategy pattern is of great importance while implementing program logic that will be invoked depending on conditions. This is just a simple example to demonstrate the pattern idea. In real time, we will be adorned with more complex situations but the underlying concept will be the same.
Comments are highly appreciated for the improvement of the topic.
Thanks for reading the article.
- 26th October, 2010: Initial post
Lead Engineer at HCL Technologies Ltd.
Code Project MVP 2012