1 of 7

Strategy Design Pattern �(Behavioral)

Thanh Le – Software Architect / Technical Consultant

Complexity: 1/3

Popularity: 3/3

2 of 7

Intent

State is a behavioral design pattern that lets you define a family of business logics, put each of them into a separate class, and make their objects interchangeable.

Image source: internet

3 of 7

Problem

One day you decided to create a navigation app for casual travelers. The app was centered around a beautiful map which helped users quickly orient themselves in any city.

The first version of the app could only build the routes over roads. People who traveled by car were bursting with joy. But apparently, not everybody likes to drive on their vacation. So with the next update, you added an option to build walking routes. Right after that, you added another option to let people use public transport in their routes.

Image source: internet

4 of 7

Solution

The Strategy pattern suggests that you take a class that does something specific in a lot of different ways and extract all of these algorithms into separate classes called strategies.

The original class, called context, must have a field for storing a reference to one of the strategies. The context delegates the work to a linked strategy object instead of executing it on its own.

The context isn’t responsible for selecting an appropriate algorithm for the job. Instead, the client passes the desired strategy to the context. In fact, the context doesn’t know much about strategies. It works with all strategies through the same generic interface, which only exposes a single method for triggering the algorithm encapsulated within the selected strategy.

���

5 of 7

Structure

Image source: internet

  • Context – maintains a reference to one of the concrete strategies and communicates with this object only via the strategy interface.

  • The Strategy interface is common to all concrete strategies. It declares a method the context uses to execute a strategy.

  • Concrete Strategies implement different variations of an algorithm the context uses.

  • The context calls the execution method on the linked strategy object each time it needs to run the algorithm. The context doesn’t know what type of strategy it works with or how the algorithm is executed.

  • The Client creates a specific strategy object and passes it to the context.

6 of 7

Pros/Cons

  • Single Responsibility :  You can isolate the implementation details of an algorithm from the code that uses it.
  • Open/Closed: You can introduce new strategies without having to change the context.
  • You can swap algorithms used inside an object at runtime.

  • If you only have a couple of algorithms and they rarely change, there’s no real reason to overcomplicate the program with new classes and interfaces that come along with the pattern
  • Clients must be aware of the differences between strategies to be able to select a proper one.�

7 of 7

Practice

https://github.com/thanhle0212/23GoF-Design-Patterns-CSharp