Design Pattern For Beginner - Part 9: Strategy Design Pattern

Welcome to the Design Pattern for Beginners article series. If you are a new reader then I suggest you visit the previous articles in this series.

Why Strategies Design Pattern?

In this article we will discuss the Strategies Design Pattern. First of all we will try to understand what the Strategies Design Pattern is and in which scenario it is useful. OK, let's think about the situation where we need to make a decision during run time. For example, we want to display a name in an application. In one form we want to display the name with the combination of name+ surname and in another form we want to display surname+ name, in this fashion.

So, how to display the name? Yes, we will make the decision at run time, as needed. Let's think of another scenario. In an automated mailing system we want to send mail to the proper person. When an error occurs, we will study the error and according to classification we would like to send one mail to the relevant department.

If it is a software related error then the mail will go to the software division, if it is a hardware related issue then the mail will go to the hardware division. Now, we don't know which type of error will occur each time. We need to make the decision at run time after analysis of the error.

Let's implement strategy classes

Let's implement our first scenario, where we want to show the combination of name and surname depending on demand (runtime). We will create a Windows application to do that. So, let's create a Windows application and provide a nice name. Then add a .cs file and place the following code into it.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. namespace Statergy  
  5. {  
  6.     public abstract class clsStatergy  
  7.     {  
  8.         public abstract  
  9.             String Display(String Name, String Surname);  
  10.     }  
  11.     public class clsNameSurname : clsStatergy  
  12.     {  
  13.         public override String Display(String Name, String Surname)  
  14.         {  
  15.             return Name + Surname;  
  16.         }  
  17.     }  
  18.     public class clssurnameName : clsStatergy  
  19.     {  
  20.         public override String Display(String Name, String Surname)  
  21.         {  
  22.             return Surname + Name;  
  23.         }  
  24.     }  
  25.     public class clsDisplayName  
  26.     {  
  27.         public String Name = "";  
  28.         public String Surname= "";  
  29.         private clsStatergy objStatergy;  
  30.         public void setStatergy(clsStatergy obj)  
  31.         {  
  32.             objStatergy = obj;  
  33.         }  
  34.         public String Show()  
  35.         {  
  36.             return objStatergy.Display(Name, Surname);  
  37.         }  
  38.     }  

Let's explain the code above .At first we created one abstract strategy class, it's nothing but to bring uniformity to the application. Then we have derived two types of display classes and have implemented (better to say overrode) the Display() function in each one of them.

Then another class called clsDisplayName will make the decision which displays the strategy needed to follow.

Implement client

Now it's time to implement the client section. Use one Windows Forms form and paste the following code into it.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using Statergy;  
  9. namespace WindowsStatergy  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void btnNameSurname_Click(object sender, EventArgs e)  
  18.         {  
  19.             clsDisplayName obj = new clsDisplayName();  
  20.             obj.Name = txtNumber1.Text;  
  21.             obj.Surname = txtNumber2.Text;  
  22.             obj.setStatergy(new clsNameSurname());  
  23.             txtResults.Text = obj.Show().ToString();  
  24.         }  
  25.         private void btnSurnameName_Click(object sender, EventArgs e)  
  26.         {  
  27.             clsDisplayName obj = new clsDisplayName();  
  28.             obj.Name = txtNumber1.Text;  
  29.             obj.Surname = txtNumber2.Text;  
  30.             obj.setStatergy(new clssurnameName());  
  31.             txtResults.Text = obj.Show().ToString();  
  32.         }  
  33.     }  

Here is the design mode.

DesignPattern1.jpg

When the user clicks the Name+Surname button after giving the name and surname, it will show the proper combination. If the user needs the surname first then they will use the Surname+Name button.

If we observe the button's click event of each button, we will find that we are creating an object during run time and the object is being set to the strategy class.

Here is the working output of when we press the Name+Surname button.

DesignPattern2.jpg

If we press Surname+Name then the output will show as in the following.

DesignPattern3.jpg