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
4
votes
3 answers

How to efficiently implement a strategy pattern with spring?

I have a web application developped in Java 1.5 with Spring framework. Application contains "dashboards" which are simple pages where a bunch of information are regrouped and where user can modify some status. Managers want me to add a logging…
Anth0
  • 3,004
  • 6
  • 26
  • 36
4
votes
3 answers

Extending functionality of Strategy pattern

I am developing an app that compares files. I decided to use the Strategy design pattern, to handle different formats, so I have something like this: public class Report { CompareStrategy strategy; ... } public interface CompareStrategy { …
DCzo
  • 141
  • 2
  • 14
4
votes
0 answers

Proper use of Strategy Pattern for simple networking layer in Swift 3

Does the following code demonstrate proper use of Strategy design pattern for a simple networking layer in swift 3? Some code smells I'm unsure about: violates Single responsibiility principle. Each strategy class such as Find, has a method for…
user6354073
4
votes
2 answers

Strategy pattern with different method signatures

Using the strategy pattern, how can treat different exectute methods differently in terms of them having different parameters? 3 example strategies public function execute(string $param1, string $param2) { // Do something specific to this…
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
4
votes
1 answer

Is it ok to use Strategy pattern in a Builder Pattern

I am curious to know if it is OK (Normal) to use Strategy pattern to change the behavior of a Builder object ? This is an example. Lets say we have the following classes and I want to use the builder to create certain type of view model for my web…
Raha
  • 1,959
  • 3
  • 19
  • 28
4
votes
4 answers

Java - Design Pattern to solve multiple conditionals logic

Say I have a Utility class. In this class, I am exposing only 2 public functions: public static boolean checkCustomerEligiblity(HttpServletRequest request) public static boolean checkCartEligiblity(HttpServletRequest request) Each of these method’s…
BrownRecluse
  • 1,638
  • 1
  • 16
  • 19
4
votes
5 answers

Extending a protocol to implement different behaviour

Swift question, Say for example you have a protocol Bark: protocol MakeSound { func bark() } A super class Dog, that implements bark and also swim: class Dog: MakeSound { } Then different types of dog that extend that: class Poodle: Dog…
Ben Smith
  • 521
  • 3
  • 15
4
votes
4 answers

Can anyone explain how the Strategy Pattern relates to Inversion of Control?

Can anyone explain exactly how the Strategy Pattern relates to Inversion of Control?
Shane Miskin
  • 1,911
  • 2
  • 22
  • 30
4
votes
2 answers

How to remove the circular dependency from this use of the Strategy pattern?

I'm trying to grok the Strategy pattern and came up with the following example: I'd like to create new games based on chess except with pieces that move differently. I also want to use the Strategy pattern to inject behaviors (how they can move)…
petabyte
  • 1,487
  • 4
  • 15
  • 31
4
votes
4 answers

How can dynamic behaviors change state in Java?

I'm working with students in my Java class on a simple Zork-like environment in which the player goes from location to location encountering items. The items should have dynamic behaviors, so that a book is readable until you burn it, or a duck can…
Andy James
  • 41
  • 3
4
votes
4 answers

Strategy pattern in C++. Implementation options

Here's a simplified example of what is called (I hope - please, correct me if I'm wrong) Strategy pattern: there's a class FileWriter which writes key-value pairs to a file and uses object of IFormatter interface for formatting text being written.…
peetonn
  • 2,942
  • 4
  • 32
  • 49
4
votes
4 answers

Best way to deal with conflated business and presentation code?

Considering a hypothetical situation where an old, legacy presentation library has been maintained over the years, and has gradually had more and more business logic coded into it through a process of hasty corrections and lack of proper…
bwerks
  • 8,651
  • 14
  • 68
  • 100
4
votes
2 answers

TDD with Strategy Pattern

I'm trying to implement the strategy pattern using TDD. Each strategy item implements an interface. What's the best way to do this with TDD? Do you have to create a test fixture for each implementation of the interface testing the same methods but…
ChoccyButton
  • 115
  • 9
4
votes
3 answers

Design pattern for cost calculator app?

I have a problem that I’ve tried to get help for before, but I wasn’t able to solve it then, so I’m trying to simplify the problem now to see if I can get some more concrete help with this because it is driving me crazy… Basically, I have a working…
Anders
  • 12,556
  • 24
  • 104
  • 151
4
votes
1 answer

Capturing delegates in anonymous methods

Consider Action _captureAction; private void TestSimpleCapturedAction() { Action action = new Action(delegate { }); Action printAction = () => Console.WriteLine("Printing..."); action += printAction; …
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198