Strategy Design Pattern - What it is ? Why it is needed ? - C#

Posted on 2011-10-24
Medium Priority
Last Modified: 2013-11-11

Can you please explain to me in simple words:

What is Strategy Design Pattern ?
Why it is needed ?

Please do NOT provide me links / articles.

Question by:milani_lucie
LVL 37

Expert Comment

ID: 37018554
A strategy design pattern is a design pattern where it is not known ahead of time what algorithms will need to be run on whatever data may be coming into the system.
All the algorithms that may need to be used are compiled into the program and then the characteristics of the data or the users choices determine which algorithm should run.

Accepted Solution

jimmycdinata earned 2000 total points
ID: 37022388
Let's explain it the easy way :

You have a class Car() with a method run() so you use it this way in a pseudo language :

mycar = new Car()

Now, you may want to change the run() behavior on the fly, while the program is executing. E.G : to simulate a motor failure or the use of a "boost" button in a video game.

There are several ways to do that : using conditional statements and a flag variable is one of them. The strategy pattern is another, that delegate the behaviour of the run() method to a subclass :

Class Car()
    this.motor = new Motor(this)

    // passing "this" is important for the motor so it knows what it is running

    method run()

    method changeMotor(motor)


If you want to change the car behavior, you can just change the motor (easier in a program that in the real life, right ;-) ?)

It's very useful if you have a lot of complex states : you can change and maintain them much more easily.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question