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.
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Statergy
- {
- public abstract class clsStatergy
- {
- public abstract
- String Display(String Name, String Surname);
- }
- public class clsNameSurname : clsStatergy
- {
- public override String Display(String Name, String Surname)
- {
- return Name + Surname;
- }
- }
- public class clssurnameName : clsStatergy
- {
- public override String Display(String Name, String Surname)
- {
- return Surname + Name;
- }
- }
- public class clsDisplayName
- {
- public String Name = "";
- public String Surname= "";
- private clsStatergy objStatergy;
- public void setStatergy(clsStatergy obj)
- {
- objStatergy = obj;
- }
- public String Show()
- {
- return objStatergy.Display(Name, Surname);
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using Statergy;
- namespace WindowsStatergy
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnNameSurname_Click(object sender, EventArgs e)
- {
- clsDisplayName obj = new clsDisplayName();
- obj.Name = txtNumber1.Text;
- obj.Surname = txtNumber2.Text;
- obj.setStatergy(new clsNameSurname());
- txtResults.Text = obj.Show().ToString();
- }
- private void btnSurnameName_Click(object sender, EventArgs e)
- {
- clsDisplayName obj = new clsDisplayName();
- obj.Name = txtNumber1.Text;
- obj.Surname = txtNumber2.Text;
- obj.setStatergy(new clssurnameName());
- txtResults.Text = obj.Show().ToString();
- }
- }
- }
Here is the design mode.
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.
If we press Surname+Name then the output will show as in the following.