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

Succinct expression of Strategy Pattern in Java

I want to use the strategy pattern in Java. However, my boss doesn't like it if I add lots of files to the code base or write verbose code. It seems that with the strategy pattern I'd have to make one file for an interface and then five additional…
Joe
  • 7,922
  • 18
  • 54
  • 83
0
votes
1 answer

Abstract Factory as a Strategy?

I would like to know whether it is possible to use an abstract factory as a strategy, e.g. nesting the two patterns and call the factory class a strategy as well. I provided an example to illustrate my question. The class ShoppingMall would be the…
0
votes
1 answer

extended Strategy Pattern with Parameters: How to set values

So basically I have different scientific models (algorithms) for calculating a certain value. Each algorithm can have a different set of Parameters to fine-tune the model. These parameters must be changeable by the user. (for now it will be a simple…
beginner_
  • 7,230
  • 18
  • 70
  • 127
0
votes
1 answer

Extending Properties failing

Properties properties = AppConfigurationManager.getInstance().getProperties(ObjectContainer.class); I have this code that populates properties. i want to decorate this for validation for one field. public class PropertiesDecorator extends…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
0
votes
2 answers

Parsing match operator and refactoring switch cases

I need a function that will take operands/operators as parameters and provide the evaluation result. The problem that I am facing is how to elegantly parse an operator. Sample code is as below internal static bool Evaluator(double operand1, double…
Tilak
  • 30,108
  • 19
  • 83
  • 131
0
votes
2 answers

How to properly implement the Strategy Pattern with two interfaces?

I have created a service data access layer where there are multiple databases where data needs to come from. I was doing fine with one database where I defined the memberRepository that contained member details. However, now I have to get…
ElHaix
  • 12,846
  • 27
  • 115
  • 203
-1
votes
1 answer

Strategy pattern that accesses the user's members

I have some trouble finding appropriate patterns for what I want to do. I have a block game with two game modes. In one mode, any removed blocks are replaced by new blocks dropped from the top of the screen. In the other mode, removed blocks are…
Cerno
  • 23
  • 2
-1
votes
1 answer

MACD + histogram default tradingview

Hello can anyone help me I'm new at making indicators can anyone give me the default code of the macd in tradingview like with the histogram color buying or selling pressure weakening. thanks I've tried it multiple times replicating the default macd…
-1
votes
2 answers

How to avoid duplicate code while using Strategy Design Pattern?

I am new to design patterns and thinking about using Strategy design pattern for implementing code in my backend service. However, the Strategies are having duplicate code. I have the following classes:- class StrategyA implements Strategy { …
Sai
  • 109
  • 8
-1
votes
1 answer

Want to access previous trades data before entering a new data

I want to access trades data of last 3 months in pine script before entering a new date , how can i do that
-1
votes
1 answer

I'm having a little trouble implementing the Strategy pattern

I confess that I'm having a bit of difficulty implementing the strategy pattern in a program that aims to display a different message daily (Ex. Message of the day...), but on special dates there may be some variations (Ex. Merry Christmas : Message…
-1
votes
1 answer

How to avoid creating new instance of class inside controller which is implemented through Strategy Pattern?

I have implemented a sort of Strategy Pattern for implementing a business rule but having trouble with figuring out how to implement it following dependency Injection: What I have done is I have calculated priority based on Business types…
-1
votes
1 answer

How do you force a subclass of an Abstract Class to be Abstract as well?

I would like to use the Strategy Design Pattern, however, I'd like to force my top-level Strategy class to enforce a policy that every Strategy subclass is also Abstract so that I can trust my developers to generalize more carefully.
Paul McElroy
  • 373
  • 1
  • 2
  • 13
-1
votes
2 answers

Making decision based on generic type in java

I have a generic class which has a method getOutput. I want to make decision based on the type of T (the class type). I tried the below but it does not work. class Test implements Interface1 { public T getOutput(String input) { if…
Zack
  • 2,078
  • 10
  • 33
  • 58
-1
votes
2 answers

different return type and signature in Strategy Pattern in Java

I am bit confuse to use Strategy Pattern in my below case. I have MyEntity class which is below. public class MyEntity { private long id; private long userid; String firstname; String lastname; // other fields I have below scenario…
Dhaval Goti
  • 447
  • 2
  • 10
  • 25