Strategy Design Pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern
  • defines a family of algorithms,
  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family.


UML Class Diagrams

Figure 1 and Figure 2 show the class diagrams of strategy pattern.
Figure 1: Strategy pattern in LePUS3

Figure 2: Strategy Pattern in UML


Participants

Class/Interface Description
Context A class that sets in between the client and the ConcreteStrategy. It handles the communication between the 2. The client cannot directly instantiate the strategy. It must notify the Context on what ConcreteStrategy to be used.
Strategy An interface that defines the contracts to be use by the ConcreteStrategy.
ConcreteStrategyA, ConcreteStrategyB  A concrete class that implements the Strategy. They contains the 
Client  It consumes the pattern.

Implementation : Sort Algorithms

To explain this better i believe that reinforcing it with an example is the best way to do. We are going to build an application that sorts numbers with different algorithms.

The Problem

How to create sorting application that adheres to the following:
  • Use single interface to use different kinds of sort algorithm.
  • The client program will not breakdown even thought there are updates or additional algorithms.
  • Encapsulate the sorting logic.
Solution

Strategy Design Pattern

The Source Code

Download Source Code


The Project Structure

We are going to use Console Application in C# using Visual Studio 2013. We need 5 classes to build this application. Figure 3 show the Project of what we will be building. Figure 4 shows the UML Diagram.

Figure 3: The Project Structure

Figure 4: The UML Class Diagram


Class Mapping
The interface and classes used in relation to the pattern is as follows:
  • Context - SortContext.cs
  • Strategy - ISortStrategy.cs
  • ConcreteStategies - BubbleSort.cs, HeapSort.cs
  • Client - Program.cs
How to Implement Strategy Design Pattern In This Situation
  1. The ISortStrategy interface has a SortNumbers method to be implemented by the BubbleSort and HeapSort classses. It will be used in the SortContext class as a property. The reasons we use interface is for extensibility and testability.
  2. SortContext class is base class that controls the flow of information between the client and the sort algorithms. It talks to the client, takes what kind of algorithm the client requested, look for the sort algorithm and sends back the output.
  3. BubbleSort and HeapSort are concrete classes that contains sort algorithms.
  4. The Program.cs is the client program.
Don't Break the Open/Closed Principle(OCP)

In order to retain the base class untouched when we add new sort algorithm, we will use Dependency Injection Principle(DIP). We will inject the new instance of algorithm in the client side instead of hard coding it in the SortContext class. In this situation Property Injection is used.

The Codes

ISortStrategy.cs
BubbleSort.cs
HeapSort.cs
SortContext.cs
Program.cs

Your thoughts!