Jeremy Shanks
Spread the Knowledge!

Category Archives for Behavioral

The Strategy Pattern


Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

How does it work?

Think of one of those nice digital cameras with the interchangeable lenses like the one pictured below. You can change the lenses on that camera depending on the type of shot you are taking. Some lenses are better for close ups, while others are better for zooming far away. Either way, you can use the same camera for all of the pictures and all you have to do is change the lense.

This is basically how the Strategy Pattern works except in the example above, the camera is referred to as the Context and the camera lense is referred to as the Strategy. You can use the same context (camera) for multiple interchangeable strategies (lenses).

Why is this useful?

  • It can help us combine similar classes into one and extract the behavior so that it can be interchangeable.
  • We can interchange behavior or strategy even at runtime.
  • The Context class does not need to know how the Strategy works, it just uses the common interface.

Example Class Diagram


Example Code

For this example, we are going to mock out a player movement strategy that could be used in a game where a player would need to walk or run.

Note: The sample code is in C#. Now let’s get started.

First we need to create the interface for Stratege. Take another look at the UML Class diagram above for reference if you need to.

Now we will create the two movement behavior classes. Notice that they implement the IMoveStrategy interface that we implemented above.

Now that we have our behaviors, we can implement our player class. Take a close look at how we can change the move strategy at runtime.

Now it’s time to use the player and its strategies.

In the above code, we are creating a new Player and both the Walk and Run Strategies. We are then using the method SetMoveStrategy() to change the behavior of the MovePlayer method. If we wanted to create a new FlyStrategy it would be very easy to do so and the player could then be able to fly along with walking and running.

I hope you have found this post helpful in learning the Strategy Pattern. If so, please share this with your fellow developers so that they too can learn.

The Observer Pattern in C#


Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.Continue reading