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
34
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
  • 39,458
  • 69
  • 187
  • 265
32
votes
0 answers

Is dependency injection just another name for the strategy pattern?

Are these terms equal or are there any important differences between dependency injection and the strategy pattern? To me it seems like Martin Fowler just renamed the strategy pattern with a catchier name, am I missing something?
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
31
votes
2 answers

Strategy pattern with spring beans

Say I'm using spring, I have the following strategies... Interface public interface MealStrategy { cook(Meat meat); } First strategy @Component public class BurgerStrategy implements MealStrategy { @Autowired CookerDao cookeryDao; …
David
  • 19,577
  • 28
  • 108
  • 128
30
votes
8 answers

How to use fields in java enum by overriding the method?

The task is to implement beautiful strategy design pattern with the java enum: public enum MyEnum { FIRST { @Override public String doIt() { return "1: " + someField; //error } }, SECOND { …
Mark
  • 17,887
  • 13
  • 66
  • 93
29
votes
5 answers

Using a Strategy and Factory Pattern with Dependency Injection

I am working on a side project to better understand Inversion of Control and Dependency Injection and different design patterns. I am wondering if there are best practices to using DI with the factory and strategy patterns? My challenge comes about…
26
votes
4 answers

Dependency Injection and the Strategy Pattern

There is an enormous amount of discussion on this topic, but everyone seems to miss an obvious answer. I'd like help vetting this "obvious" IOC container solution. The various conversations assume run-time selection of strategies and the use of…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
25
votes
6 answers

C++ Strategy pattern

In the past, I have seen the strategy pattern explained as a mechanism which allows the user of a function/class to provide their own functionality for that function/class. I had always been taught that the way to implement the pattern was by taking…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
25
votes
1 answer

Strategy Pattern and Dependency Injection using Unity

I am finally getting my feet wet with Dependency Injection (long overdue); I got started playing with Unity and run into an issue with the strategy pattern. I can use the container to return to me specific implementations of a strategy based on a…
24
votes
4 answers

Difference between Strategy pattern and Delegation pattern

What is the difference between Strategy pattern and Delegation pattern (not delegates)?
hIpPy
  • 4,649
  • 6
  • 51
  • 65
19
votes
3 answers

When to use C++ private inheritance over composition?

Can you give me a concrete example when is preferable to use private inheritance over composition? Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a…
19
votes
8 answers

What's the difference between "Chain of responsibility" and "Strategy" patterns?

I'm raising this question because of another question I asked here on SO some days ago. I had to solve an specific problem, and after two replies I got, I realized two patterns can help to solve that problem (and any other similar). Chain of…
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
18
votes
3 answers

Difference between Strategy Pattern and Adapter

Why is the strategy design pattern and the adapter related to each other? It seams to me that the adapter manipulates the result of some method to fullfill the input needs of anotherone. Wheras the strategy precibes behaviour.
Thomas Zenglein
  • 193
  • 1
  • 7
18
votes
6 answers

How to specify exceptions to be thrown by an implementor of an interface?

I'm currently developing a solution and have designed it in a way such that it strongly implements the strategy/provider pattern. As such the solution exposes a number of interfaces and contains default implementations of these interfaces which can…
gouldos
  • 1,015
  • 1
  • 16
  • 30
18
votes
4 answers

Differences between Strategy Pattern and Inheritance

There is a same concept for Strategy Pattern and Inheritance, so I can implement Strategy Pattern with Inheritance that sounds it is simpler and cleaner than Strategy Pattern. Startegy Pattern: class IBase { public: virtual void…
16
votes
7 answers

How to use the Strategy Pattern with C#?

Here's what I have so far: namespace Strategy { interface IWeaponBehavior { void UseWeapon(); } } namespace Strategy { class Knife : IWeaponBehavior { public void UseWeapon() { …
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
1
2
3
48 49