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

A good design pattern for conditional statements based on the status of multiple variables

I'm working out a method to calculate a total for a shopping cart written in PHP and would like some feedback on a good design pattern for handling the different conditions. I am trying to offer the admins multiple strategies for calculating…
tollmanz
  • 3,113
  • 4
  • 29
  • 34
3
votes
1 answer

Available data in TradingView's exported chart data file

To back-test a trading strategy, I use the replay feature in trading view and mark my trades by adding a "long position" or "short position" from the left panel. Like this: I need to save the data (chart data including the positions, or any other…
3
votes
3 answers

DDD - how to enforce invariants but specific to the client requirements?

I am trying to figure out how to keep the invariants still consistent for a few consumers (business clients) of the project who have their own requirements on the same version of the aggregate root. Let's take the Customer as an example and ask…
3
votes
1 answer

Pinescript strategy.entry long/short seem delayed by an additional 2 candles

When I enter a strategy.entry position, when I look at the graph that shows the color change for a condition, the actual buy order does not execute until the third candle of the color and vice versa when selling, i.e., it is the third red candle…
Yoyofanusa
  • 31
  • 1
  • 2
3
votes
2 answers

Strategy Pattern and context class

Reading about the strategy design pattern, was thinking if it is possible to have two context. Looking forward for opinions if the below code-design utilises the Strategy design pattern. Or strategy design pattern forces us to have only one context…
IonKat
  • 436
  • 6
  • 12
3
votes
2 answers

Can someone show me a simple example of strategy pattern using scheme?

I am new to design pattern and I am trying to learn the strategy pattern. After reading bunch examples here and on oodesign.com, I have a fair understanding of its intent. However, most example I found are in Java, C# or, C/C++; these languages…
Jackal
  • 33
  • 2
3
votes
3 answers

How to correctly implement strategy design pattern

I'm trying to implement strategy design pattern, and want to know if I do it correctly. Lets say, I have class FormBuilder which uses strategy from list below to build the form: SimpleFormStrategy ExtendedFormStrategy CustomFormStrategy So the…
carcade
  • 107
  • 2
  • 5
3
votes
1 answer

How to add conditions dynamically according to the different requirement?

I am writing a code in Java to enable different components. Every component needs to satisfy a certain set of conditions to get enabled. Every condition is basically a method which performs some logic & returns boolean. Currently, I have written a…
Ryan Gusto
  • 179
  • 5
3
votes
3 answers

Design OOP question on Decorator and Strategy pattern C#

Say for example you have a base abstract class public abstract Foo { IFlyable _fly; ISwimmable _swim; void performSwim() { _swim.swim(); } void performFly() { _fly.fly(); } } And have behaviors/algorithm that you will have in…
None
3
votes
1 answer

design pattern for switching email providers in the code

We need to send emails in our php app (who doesn't). Initially when our app was in infancy, we used simply linux sendmail. A bit moving forward we switched to our own SMTP server. That means code change in every file that has email related…
Waku-2
  • 1,136
  • 2
  • 13
  • 26
3
votes
3 answers

Strategy Pattern- Correct Implementation

I'm using this pattern for the first time, and using C#. I just wanted to check that this is the correct implementation. I have a button on a Winform, and when clicked on that will output some data in a particular format, defined by choosing from a…
Darren Young
  • 10,972
  • 36
  • 91
  • 150
3
votes
3 answers

Using strategy design pattern with an abstract parameter

I am currently working on a pretty simple project to improve my SOLID and Design Patterns Knowledge. The idea was to create a "Smart Lock" for a door that can recognize a person by different parameters such as fingerprints, facial recognition,…
3
votes
3 answers

Using the Strategy Pattern to avoid downcasting

I was reading on this site about the Liskov substitution principle. It states: As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes…
3
votes
2 answers

Strategy pattern movies conditionals from inside the main class to the client code, so what's the point?

I am trying to understand the strategy pattern, here is an example, we have a Customer that has a method GetDiscount, this method is written as follows GetDiscount If (Condition1) Return 10% else (Condition2) Return 20% Now…
Sisyphus
  • 900
  • 12
  • 32
3
votes
1 answer

Implement Strategy Pattern in C++ without Pointers

My goal is to have a C++ Implementation of the Strategy Pattern without manual memory allocation. I do not feel like it ought to be necessary in the example that I give, conceptually speaking. Also, manual memory management is prone to errors. Keep…
Jesko Hüttenhain
  • 1,278
  • 10
  • 28