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.

728 questions
271
votes
21 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,799
  • 2
  • 18
  • 6
220
votes
15 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)
209
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
  • 25,619
  • 25
  • 85
  • 120
136
votes
15 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,466
  • 2
  • 10
  • 10
125
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
  • 19,057
  • 21
  • 82
  • 129
117
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
  • 1,173
  • 2
  • 8
  • 4
112
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
  • 7,154
  • 16
  • 68
  • 80
111
votes
19 answers

Real World Example of the Strategy Pattern

I've been reading about the OCP principle 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
71
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
  • 3,192
  • 7
  • 39
  • 68
66
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
  • 51,063
  • 11
  • 80
  • 130
63
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
  • 4,078
  • 3
  • 28
  • 54
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…
51
votes
12 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
  • 17,065
  • 5
  • 54
  • 72
40
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
  • 403
  • 1
  • 4
  • 4
36
votes
5 answers

Strategy Pattern with different parameters

I came across a problem when using the strategy pattern. I am implementing a service for creating tasks. This service also resolves the responsible clerk for this task. Resolving the clerk is done by using the strategy pattern because there are…
Daniel
  • 835
  • 1
  • 7
  • 18
1
2 3
48 49