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
15
votes
2 answers

what's the difference between the patterns Strategy, Visitor and Template Method?

I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (:
15
votes
2 answers

A Strategy against Policy and a Policy against Strategy

When I first discovered the Strategy pattern, I was amazed of the seemingly endless possibilities it offered to me and my programs. I could better encapsulate my models' behaviour and even exchange this behaviour on the fly. But the strategy could…
mstrobl
  • 2,381
  • 14
  • 16
15
votes
4 answers

Does this Java Strategy pattern have a redundant Context class?

The following code sample is an implementation of the Strategy pattern copied from Wikipedia. My full question follows it... The Wiki's main method: //StrategyExample test application class StrategyExample { public static void main(String[]…
John K
  • 28,441
  • 31
  • 139
  • 229
15
votes
4 answers

Strategy Pattern vs Dependency Injection

How is strategy pattern is different then dependency injection? ie below is what you can do with Strategy pattern: class Foo{ private readonly ISortAlgo _sortAlgo; public Foo(ISortAlgo sortAlgo) { _sortAlgo = sortAlgo; } public…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
14
votes
3 answers

Better alternative to Strategy pattern in Scala?

When I'm programming in Java (or a similar language), I often employ a simple version of the Strategy pattern, using interfaces and implementation classes, to provide runtime-selectable implementations of a particular concept in my code. As a very…
MattK
  • 10,195
  • 1
  • 32
  • 41
13
votes
3 answers

Java fallback pattern

I'm trying to find a nice way of implementing a service which relies on a third-party library class. I also have a 'default' implementation to use as fallback in case the library is unavailable or can not provide an answer. public interface Service…
13
votes
6 answers

what is the difference between visitor and strategy pattern?

I have learned both the patterns but did not understand the differences between these two patterns. I do not know scenarios, when and where to use these patterns. Can any one explain the differences and use cases?
12
votes
2 answers

Java equivalent of .NET Action and Func, etc

Are there any standard generic "callback" or "function/method" types in Java, like System.Action or System.Func in .NET? In my concrete case, I need a class that wraps a method that takes one (generic) parameter of type T and returns nothing…
Christian Klauser
  • 4,416
  • 3
  • 31
  • 42
12
votes
2 answers

How to create a strategy pattern in Objective-C?

I need to develop a strategy pattern where i have a main class with other three classes where i need to refer to the objects of the other three classes using the main class object.To solve this is the strategy pattern will help me? If so please do…
Cathy
  • 797
  • 2
  • 14
  • 25
12
votes
4 answers

In the strategy pattern can the strategy take the Context as parameter

Feedback summary I will now close this thead (I think there will be no more feedback) and try to summarize what I understood using the "Context" as a parameter for my strategy introduces a tight coupling that should be avoided and also could force…
FrenchData
  • 630
  • 5
  • 10
12
votes
8 answers

Where is the benefit in using the Strategy Pattern?

I've looked at this explanation on Wikipedia, specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes…
slashmais
  • 7,069
  • 9
  • 54
  • 80
11
votes
3 answers

Strategy Design Pattern- choosing between strategies with counters

I am programming in Java but this is a more of a design question so any OO programmer could probably answer this question. I have a question concerning the Strategy design pattern. Here are several inks I have found useful: Strategy Pattern…
Matthew Kemnetz
  • 845
  • 1
  • 15
  • 28
11
votes
5 answers

Saving Data with the Factory Pattern?

I've been becoming more familiar with the Factory Pattern (along with Strategy Pattern) and what a great benefit the pattern can have. However, I've been struggling with the following situation: Previously, I would have done something like the…
11
votes
2 answers

Bridge- vs Strategy-Pattern

I know, this question was asked many times, but I did some research and still don't get it, probably you can help me out: As stated many times, the UML is almost the same. Also the implementation and idea is more or less the same: Instead of…
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
10
votes
5 answers

Best way to do this generic abstract class in c#?

I know I'm not doing this right, but I also know there is a way to do this. I'm trying to be as generic and abstract as possible, otherwise my code is going to get real messy. So I'm using strategy pattern here as well, which is the…
apexdodge
  • 6,657
  • 4
  • 26
  • 33
1 2
3
48 49