How to use the strategy design pattern in .Net

Source: InfoWorld

Take advantage of the strategy design pattern to encapsulate a set of algorithms inside a class and apply them based on input

Design patterns are used to solve common design problems in software development and to reduce the complexities in our source code. Design patterns can be creational (related to object creation), structural (related to object assembly), or behavioral (related to object collaboration and separation of responsibilities).

The strategy design pattern is a Gang-of-Four (GoF) design pattern. It is a behavioral pattern used to define a family of algorithms and encapsulate each of them inside a class. These encapsulated algorithms are then interchangeable — i.e., they can vary based on different inputs, independently of the client that uses them.

In other words, the strategy design pattern decomposes implementation code into concrete classes and hence promotes reusability. This article presents a discussion of the strategy design pattern and how it can be implemented in C#.

Strategy design pattern participants

The participants in a typical implementation of the strategy design pattern include the following:

Strategy — used to declare a common interface for all concrete strategy types. The Context takes advantage of the Strategy interface to invoke an algorithm at runtime.

ConcreteStrategy — used to implement the algorithm that is declared in the Strategy interface.

Context — contains a reference to the Strategy instance and uses this reference to invoke the algorithm that is defined by a concrete strategy type.

Pentechs've been excellent and a pleasure to work with.  Despite the complexity of the project they've delivered a superb result and kept all their promises.

CEO, Digiclips, UK

See what our clients say

Want to start a project?

Contact us at

+91 886 110 9173

+91 997 289 6003

+91 80 4200 7229