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

What design/pattern to use for a Client application using multiple providers?

This is a design related question. Lets say we have a public API called ClientAPI with a few web methods like CreateAccount, GetAccount. Depending on the customer, we use a number of different providers to fulfil these requests. So say we have…
6
votes
3 answers

Parametrized Strategy Pattern

I have several Java classes which implement the strategy pattern. Each class has variable number parameters of different types: interface Strategy { public data execute(data); } class StrategyA implements Strategy { public data…
user141335
6
votes
2 answers

Strategy Design pattern with IOC containers - Ninject specifically

I have a class which is going to need to use the strategy design pattern. At run time I am required to switch different algorithms in and out to see the effects on the performance of the application. The class in question currently takes four…
Finglas
  • 15,518
  • 10
  • 56
  • 89
6
votes
4 answers

Strategy pattern in F#

In C# I have the following code: public class SomeKindaWorker { public double Work(Strategy strat) { int i = 4; // some code ... var s = strat.Step1(i); // some more code ... var d = strat.Step2(s); …
CSJ
  • 3,641
  • 2
  • 19
  • 29
6
votes
2 answers

Polymorphic Enums for state handling

how do i handle Enums without using switch or if statements in C#? For Example enum Pricemethod { Max, Min, Average } ... and i have a class Article public class Article { private List _pricehistorie; public…
6
votes
1 answer

Strategy Pattern or Interface?

I'm looking to abstract a helper method. The method needs to be able to take in an object, do things with it depending on the type of object, and return a value. Would it be better to do something like this: interface ICanDo { string…
Josh
  • 632
  • 7
  • 13
6
votes
1 answer

How can a delegate respond to multiple events with a generic and extensible class?

I have rigged up a technique to handle multiple subreports in an rdlc report, but as I have tried to make it generic and repeatable, I have instead had to take the model and tweak it slightly for each case. For example, if I define an abstract…
bentaisan
  • 1,056
  • 3
  • 12
  • 29
5
votes
2 answers

Using enums as a container of implementations

I'm currently working on a project where we have to represent a set of vectors in a 3D environment. We have several different visualization implementations. I came to the idea, that I could bundle all the visualization types in an enum. I have…
Prine
  • 12,192
  • 8
  • 40
  • 59
5
votes
4 answers

How do you pass 'this' as an argument to another class constructor without circular dependencies?

I'm thinking specifically of the Strategy pattern (Design Patterns, GoF94), where it is suggested that the context passed to the strategy constructor can be the object which contains the strategy (as a member) itself. But the following won't…
Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
5
votes
3 answers

Avoiding coupling with Strategy pattern

I am attempting to apply the Strategy pattern to a particular situation, but am having an issue with how to avoid coupling each concrete strategy to the context object providing data for it. The following is a simplified case of a pattern that…
drharris
  • 11,194
  • 5
  • 43
  • 56
5
votes
3 answers

Merging duplicate code that use different objects

I use two api calls to get data about vehicleUtils depending on contentFilter. I have very similar code for both (drivers and vehicles). What i tried to do is to extract the code into a single method and apply Strategy pattern like they suggest here…
5
votes
1 answer

Angular: lookup all providers implementing a specific interface

In my Angular (4+) application, I want to create a basic plugin mechanism: The extension point is defined as an interface Extensions implement that interface To find all extensions, I need to discover the implementations of that interface at…
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
5
votes
1 answer

Objective C - Strategy Pattern?

I understand the concept of the "strategy pattern" but I am still a little bit confused. Let'say we have a class named Dog. Dog has MovementBehaviour (interface) which can be MovementBehaviourNormal and MovementBehaviourFast. MovementBehaviourNormal…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
5
votes
3 answers

Is this code too brittle?

I need to create a strategy pattern where a user selects four strategies from a list of twenty or thirty unique strategy objects. The list of strategies will be expanded as the project matures, and users can change their selected strategy at any…
Stephen
  • 18,827
  • 9
  • 60
  • 98
5
votes
1 answer

How to avoid lots of if-else in javascript (nodejs)

Based on a parameter, function should select a json file out of more 100 json and fire a query to other system. There will lots of query around in hundreds. Obviously if else and switch won't be manageable. I took a look for strategy patten in the …