Questions tagged [strategy-pattern]

The Strategy pattern (also known as the policy pattern) is a design pattern whereby an algorithm's behavior can be selected at runtime. It is one of the Gang of Four's behavioral design patterns. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The intent of the strategy pattern is to "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it."

The pattern relies heavily on composition. An important advantage of this approach over inheritance and simple method overrides is that the behavior can be freely changed at run-time by changing the object implementing it.

This pattern is one of the original described in "Design Patterns" by Gamma, Helm, Johnson and Vlissides.

638 questions
236
votes
20 answers

What is the difference between Strategy design pattern and State design pattern?

What are the differences between the Strategy design pattern and the State design pattern? I was going through quite a few articles on the web but could not make out the difference clearly. Can someone please explain the difference in layman's…
Chin Tser
  • 2,451
  • 2
  • 16
  • 6
175
votes
19 answers

What is the difference between the template method and the strategy patterns?

Can someone please explain to me what is the difference between the template method pattern and the strategy pattern is? As far as I can tell they are 99% the same - the only difference being that the template method pattern has an abstract class as…
Calanus
  • 23,416
  • 24
  • 77
  • 118
162
votes
12 answers

What is the difference between Factory and Strategy patterns?

Can any one explain the difference between factory and strategy patterns? For me both are looking same other than an extra factory class (which create an object of product in factory patterns)
123
votes
14 answers

What is the difference between the bridge pattern and the strategy pattern?

I tried to read many articles on dofactory, wikipedia and many sites. I have no idea on differences between bridge pattern and the strategy pattern. I know both of them decouple an abstraction from its implementation and can change implementation…
Krirk
  • 1,336
  • 2
  • 8
  • 10
121
votes
7 answers

Using a strategy pattern and a command pattern

Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern takes in parameters for execution, while the Command pattern doesn't. It…
Extrakun
  • 18,259
  • 19
  • 74
  • 122
100
votes
17 answers

Real World Example of the Strategy Pattern

I've been reading about the OCP principal and how to use the strategy pattern to accomplish this. I was going to try and explain this to a couple of people, but the only example I can think of is using different validation classes based on what…
Tired
99
votes
9 answers

What is the difference between Strategy pattern and Dependency Injection?

Strategy pattern and Dependency Injection both allow us to set / inject objects at run time. What is the difference between Strategy pattern and Dependency Injection?
Nero
  • 993
  • 1
  • 7
  • 4
83
votes
6 answers

Difference between Strategy pattern and Command pattern

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.
Krishna
  • 6,706
  • 15
  • 64
  • 79
63
votes
6 answers

How does the Strategy Pattern work?

How does it work, what is it used for and when should one use it?
Jorge Córdoba
  • 47,433
  • 11
  • 77
  • 126
57
votes
7 answers

Strategy Pattern V/S Decorator Pattern

I just came across two patterns. Strategy Pattern Decorator Strategy Pattern :- Strategy pattern gives several algorithms that can be used to perform particular operation or task. Decorator Pattern :- Decorator pattern adds some functionality…
Nirav Kamani
  • 2,985
  • 7
  • 33
  • 67
53
votes
10 answers

Polymorphism vs Strategy pattern

What is the difference between the Strategy pattern and Polymorphism in Java? I'm confused that whatever is achieved via Strategy Pattern is basically possible by polymorphism. Correct me if I'm wrong in this regard. Please, also provide me example…
TryinHard
  • 3,822
  • 3
  • 26
  • 51
52
votes
5 answers

Strategy Pattern with no 'switch' statements?

I've been doing some reading on the Strategy Pattern, and have a question. I have implemented a very basic Console Application below to explain what I'm asking. I have read that having 'switch' statements is a red flag when implementing the…
44
votes
11 answers

What is the difference between Strategy pattern and Visitor Pattern?

I have trouble understanding these two design patterns. Can you please give me contextual information or an example so I can get a clear idea and be able to map the difference between the two of them. Thanks.
dotnetstep
  • 14,826
  • 4
  • 49
  • 61
39
votes
9 answers

Strategy vs. Bridge Patterns

I know this question's been asked before (e.g., What is the difference between the bridge pattern and the strategy pattern?). However, could someone please explain, using clear-cut examples, what the difference is and in what sorts of cases one must…
S. Valmont
  • 393
  • 1
  • 3
  • 4
33
votes
10 answers

When and why should the Strategy Pattern be used?

When would the Strategy Pattern be used? I see client code snippets like this: class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies …
User1
  • 35,366
  • 60
  • 170
  • 251
1
2 3
42 43