views:

1415

answers:

8

How does it work, what is it used for and when should one use it?

A: 

Strategy pattern

Kristian
+2  A: 

Directly from the Strategy Pattern Wikipedia article:

The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.

Bill the Lizard
+18  A: 

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()
car.run()

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()
    {
        this.motor.run()
    }

    method changeMotor(motor)
    {
        this.motor=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.

e-satis
I like the answer so I voted it up but I think the code could be more elaborated.
Jorge Córdoba
I know, it´s just to illustrate the principle. There is plenty of accurate snippets beyong the links a reader can find in the other ansers.
e-satis
+7  A: 
Jorge Córdoba
It's probably just a typo, but shouldn't your CMergeSort class implement the ISort interface?
Andrew Swan
+7  A: 

I would recommend reading chapter 1 of Head First - Design Patterns. This gives an excellent explanation of how to use it, why to use it and how it is actually just applying basic Object Oriented principles. It gives a concrete (though somewhat funny) example which really shows a problematic situation and how the strategy pattern solves it.

A: 

A closely related pattern is the Delegate pattern; in both cases, some of the work is passed to some other component. If I understand correctly, the difference between these patterns is this (and please correct me if I'm wrong):

  • In the Delegate pattern, the delegate is instantiated by the enclosing (delegating) class; this allows for code reuse by composition rather than inheritance. The enclosing class may be aware of the delegate's concrete type, e.g. if it invokes its constructor itself (as opposed to using a factory).

  • In the Strategy pattern, the component that executes the strategy is a dependency provided to the enclosing (using) component via its constructor or a setter (according to your religion). The using component is totally unaware of what strategy is in use; the strategy is always invoked via an interface.

Anyone know any other differences?

Andrew Swan
A: 

To add to the already magnificient answers: The strategy pattern has a strong similarity to passing a function (or functions) to another function. In the strategy this is done by wrapping said function in an object followed by passing the object. Some languages can pass functions directly, so they don't need the pattern at all. But other languages can't pass functions, but can pass objects; the pattern then applies.

Especially in Java-like languages, you will find that the type zoo of the language is pretty small and that your only way to extend it is by creating objects. Hence most solutions to problems is to come up with a pattern; a way to compose objects to achieve a specific goal. Languages with richer type zoos often have simpler ways of going about the problems -- but richer types also means you have to spend more time learning the type system. Languages with dynamic typing discipline often gets a sneaky way around the problem as well.

jlouis
+1  A: 

Get the simplify explanation and sample on Strategy pattern here http://tech.wowkhmer.com/post/2008/11/14/Strategy-Design-Pattern.aspx

Samnang