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
10
votes
1 answer

Varying parameters in strategy pattern

Sometimes when using the strategy pattern I find some of the algorithm implementations do not require the same parameter list. For example public interface Strategy{ public void algorithm(int num); } public class StrategyImpl1…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
10
votes
7 answers

strategy pattern in C#

I've been going through Head First Design Patterns (just came in recently) and I was reading about the strategy pattern, and it occurred to me that it might be a great way to implement a common way of calculating taxes etc. on all of the particular…
Steven Evers
  • 16,649
  • 19
  • 79
  • 126
10
votes
5 answers

Should the strategy pattern be stateless?

Must a class that is a "gang of four" strategy be completely stateless (ie no fields) or can it contain immutable state (ie final fields)?
murungu
  • 2,090
  • 4
  • 21
  • 45
10
votes
1 answer

Checking of **kwargs in concrete implementation of abstract class method. Interface issue?

I am trying to implement the Strategy design pattern to create an interface for an underlying algorithm to be implemented in a modular fashion. Currently, as per code below, I have one top-level/parent abstract class (ParentAbstractStrategy) that…
10
votes
2 answers

Unity Resolve Multiple Classes

How do I get microsoft unity to 'construct' a list of classes for a given interface type. Very Simple example: List list = new List(); list.Add(new NewYorkShippingCalculation()); list.Add(new…
dnndeveloper
  • 1,631
  • 2
  • 19
  • 36
10
votes
4 answers

How to implement strategy pattern in C++ with std::function

I'm reasing about the best way to implement the strategy pattern in C++. Up to now, I've always used the standard way, where the context has a pointer to the base strategy class as follows: class AbstractStrategy{ public: virtual void exec()…
gcswoosh
  • 1,279
  • 1
  • 15
  • 31
10
votes
0 answers

State monad and strategy pattern

I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter. All it does, in its basic implementation, is to take a some…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
10
votes
5 answers

Strategy Pattern with Different parameters in interface (C#)

I am basically trying to implement a Strategy pattern, but I want to pass different parameters to the "interfaces" implementation (that inherit from the same object) and don't know if this is possible. Maybe I'm choosing the wrong pattern, I get an…
user210757
  • 6,996
  • 17
  • 66
  • 115
9
votes
4 answers

Node.js - create object of class name specified in variable

I have a class hierarchy like: |-> Square AbstractShape -+-> Circle |-> Triangle Now, I'd like to implement strategy pattern and create an object of class that's stored in string. In PHP I'd use: $type =…
Forseti
  • 2,587
  • 6
  • 21
  • 32
9
votes
5 answers

When and How Strategy pattern can be applied instead of decorator pattern?

I am learning design patterns and trying to follow Go4 book. On page:179, in the decorator pattern chapter, there is a line which says "..by extending the number of strategies from just one to an open-ended list, we achieve the same effect as…
Sandbox
  • 7,910
  • 11
  • 53
  • 67
9
votes
6 answers

Is there a good way to avoid unused method parameter in some of the subclasses while applying strategy pattern?

I have the following scenario where I have different kinds of sales algorithms to calculate the sales price. FixedSaleStrategy does not need basePrice parameter while all the other strategy implementations need it. Is there a good way to avoid this…
derdo
  • 1,036
  • 12
  • 21
9
votes
1 answer

How can I use a static method as a default parameter for the strategy design pattern?

I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") …
9
votes
6 answers

Trying to implement a kind of traveller algorithm in Java

I'm trying to implement a simple and efficient algorithm for this kind of traveller problem (but this is NOT the "travelling salesman"): A traveller has to visit N towns, and: 1. each trip from town X to town Y occurs once and only once 2. the…
Bludzee
  • 2,733
  • 5
  • 38
  • 46
9
votes
3 answers

How to implement Strategy Pattern in Go?

Here's the the general problem I am trying to solve: One set of packages is collecting data from one source and sending it to many channels. A second set of packages is collecting data from many channels and writing it to one source. This set of…
KevDog
  • 5,763
  • 9
  • 42
  • 73
8
votes
2 answers

Template Method and Strategy design patterns

This is probably a newbie question since I'm new to design patterns but I was looking at the Template Method and Strategy DP's and they seem very similar. I can read the definitions, examine the UML's and check out code examples but to me it seem…