Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it
In this pattern you have several algorithms to do a task. Depending on your requirement you can select the algorithm dynamically. You can implement each algorithm in separate class
This is the UML diagram for Strategy Design Pattern
This is an interface which your algorithm (ConcreteStrategy) uses to implement it
Implements the algorithm using the Strategy interface.
Context is configured with a ConcreteStrategy object to make dynamic call
Maintains a reference to a Strategy object.
I will do simple example to show you how to implement this Strategy design pattern in VB.NET. We will first create list of student and try to sort this list using different type of sorting algorithms
Our Strategy here is the Sorting. I am going to select three algorithms for sorting named Merge Sort, Bubble Sort, Quick Sort. So those sorting methods are my Concrete Classes. Now lets look at the UML diagram for this example
StudentList class is our Context Class. This class has reference to SortStrategy interface and maintain the list of student for strategy.
Public Class StudentList Private mSortingStrategy As SortStrategy Private mStudents As New List(Of String) Public Sub Add(ByVal name As String) mStudents.Add(name) End Sub Public Property SortStrategy() As SortStrategy Get Return mSortingStrategy End Get Set(ByVal value As SortStrategy) mSortingStrategy = value End Set End Property Public Sub Sort() mSortingStrategy.Sort(mStudents) For Each student As String In mStudents Console.WriteLine(student) Next End Sub End Class
Our Strategy interface is the SortStrategy interface which has the Sort method
Public Interface SortStrategy Sub Sort(ByVal students As List(Of String)) End Interface
Then we have three ConcreteStrategy classes named QuickSort,BubbleSort and MergeSort. They implement the SortStrategy interface
Public Class QuickSort Implements SortStrategy Public Sub Sort(ByVal students As List(Of String)) Implements SortStrategy.Sort 'implemet QuickSort algo here for students 'default is Quick Sort students.Sort() Console.WriteLine("Quck Sorting done for Students") End Sub End Class
Public Class BubbleSort Implements SortStrategy Public Sub Sort(ByVal students As List(Of String)) Implements SortStrategy.Sort 'implemet Bubble sort algo here for students Console.WriteLine("Bubble Sorting done for Students") End Sub End Class
Public Class MergeSort Implements SortStrategy Public Sub Sort(ByVal students As List(Of String)) Implements SortStrategy.Sort 'implemet Merge Sort algo here for students Console.WriteLine("Merge Sorting done for Students") End Sub End Class
In the Client section , I have written the following code to test above Strategy Design Pattern. This will add several student to the List and do the sorting operations
Module Module1 Sub Main() 'Create context object Dim students As New StudentList() students.Add("John") students.Add("Taylor") students.Add("Mark") students.SortStrategy = New QuickSort() students.Sort() students.SortStrategy = New BubbleSort() students.Sort() students.SortStrategy = New MergeSort() students.Sort() Console.ReadLine() End Sub End Module
Now you will get the following output
Quck Sorting done for Students John Mark Taylor Bubble Sorting done for Students John Mark Taylor Merge Sorting done for Students John Mark Taylor