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

Up to what extent can you prevent modifying existing code when using design patterns?

I am taking a design patterns class in school, and have read through some chapters of Head First Design Patterns. What I'd like to find out is up to what extent can design patterns prevent rewriting of existing code. Let us take Duck classes and…
5
votes
6 answers

Which design pattern is most appropriate?

I want to create a class that can use one of four algorithms (and the algorithm to use is only known at run-time). I was thinking that the Strategy design pattern sounds appropriate, but my problem is that each algorithm requires slightly different…
Umbungu
  • 945
  • 3
  • 10
  • 30
5
votes
3 answers

Should safe pointers be used in strategy pattern?

Given a typical strategy pattern class Strategy { public: virtual int execute() const = 0; } class StrategyA : public Strategy { public: int execute() const override; } class StrategyB : public Strategy { public: int execute() const…
Daniel
  • 8,179
  • 6
  • 31
  • 56
5
votes
4 answers

Java Strategy pattern - can I delegate strategies instantiation in the Context class?

I am currently learning design patterns on my own. As I studied the Strategy pattern I found something that looks strange for me. I looked for discussions on this pattern but none answered my question... which is how can I implement the Strategy…
eqtèöck
  • 971
  • 1
  • 13
  • 27
5
votes
3 answers

Dynamic Service Strategies in AngularJS

How can I switch out a service on-the-fly and have all components (relying on the service) automatically be bound to the data on the new strategy? I have a Storage service and two storage strategies, StorageStrategyA and StorageStrategyB. Storage…
bernhardw
  • 3,239
  • 2
  • 20
  • 16
5
votes
2 answers

"Strategy Pattern" in Haskell

In the OO world, I have a class (let's call it "Suggestor") that implement something approaching a "Strategy Pattern" to provide differing implementations of an algorithm at runtime. As an exercise in learning Haskell, I want to rewrite this. The…
James Davies
  • 9,602
  • 5
  • 38
  • 42
5
votes
2 answers

Using the Strategy Design Pattern(C#) to sort based on different columns of data

I am currently trying to get my head around all of the different Design Patterns, I have been set the task of sorting an IQueryable based on different columns, this is how it is currently implemented: if (choice == 1) { return from Animals in…
JamesZeinzu
  • 235
  • 2
  • 3
  • 12
5
votes
1 answer

Mobile strategy: How to handle this server-client version permutation?

We are hosting a SAAS application for clients. The main app is a web appliction and different clients can be on different versions. e.g.: Company A version 1.0 Company B version 1.1 We also have Android/iOS apps in App Store. Since the apps are…
Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
5
votes
4 answers

What are the similarities between the Template Method and Strategy design patterns

is this an example of TemplateMethod Pattern?? public abstract class Character{ public final void useWeapon(){ useBusterSword(); useMateriaBlade(); useUltimateWeapon(); } public abstract void useBusterSword(); …
4
votes
1 answer

How can I avoid a cross-reference between 2 Objects?

I have a DataAccess Class and it has a reference to a an interface. The interface represents the caching strategy to use. I would like that the caching strategy has something like an initalize() method that fills the cache on startup. However that…
beginner_
  • 7,230
  • 18
  • 70
  • 127
4
votes
5 answers

Does the Strategy Pattern violate the Single Responsibility Principle?

If the Single Responsibility Principle states that every object must have a single reason to change and a single strategy class implemented with the Strategy pattern (by definition) has multiple methods that can change for any number of reasons,…
plaureano
  • 3,139
  • 6
  • 30
  • 29
4
votes
1 answer

Implementing strategy in nest.js

I am trying to use the strategy pattern for the service, however the Module I try to use as context for strategy seems to only stick to one of the two. Here is the example code: animal.module.ts @Module({}) export class AnimalModule { static…
4
votes
1 answer

Best way for imlement Strategy design pattern in Spring

I want implement strategy design pattern in spring boot application. I create BeanPostProcessor for construct strategy resolver: @Component public class HandlerInAnnotationBeanPostProcessor implements BeanPostProcessor { private final…
ip696
  • 6,574
  • 12
  • 65
  • 128
4
votes
4 answers

Alternative Pattern to Strategy

I have a piece of code where I started to put the strategy pattern in place, say as follows: IStrategy StrategyA : IStrategy StrategyB : IStrategy StrategyC : IStrategy The interface just has a Calculate method on it. After implementing, it turned…
Mike
  • 6,149
  • 5
  • 34
  • 45
4
votes
2 answers

Strategy Pattern with each algorithm having a different method signature

I am doing a refactor over certain code. We have a list of investors with amounts assigned to each. The total of amounts should be equal to another total, but sometimes there are a couple of cents of difference, so we use different algorithms to…
Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82