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

Using the Strategy pattern for choosing api

is this a valid use case? For example to access a calendar from multiple sources, such as facebook or google. Whereby each of these have their own strategies to call the underlying specific APIs? Due to the use of APIs and not a lower level…
0
votes
3 answers

Does my example for using the Strategy design pattern with the Template Method design pattern make sense?

For some homework, we have to devise an example (with classes) where both the Strategy and Template Method design patterns come together to complement each other and make them more customizable as a result. Or "use template method to provide more…
0
votes
2 answers

How to avoid a loop-switch anti pattern

I am writting a regex parser for which, a loop-switch anti-pattern appears to be the only approach. Please ignore actual rules of parsing, as this parsing-logic is just custom made for an internal application, and deviant from common use. public…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
1 answer

Is this is a Strategy pattern

I am new to java and learning Design Patterns. I have implemented a simple use case. a mail service, in which i want to represent the mail in different formats. 1) Plain 2) XML 3 ) HTMl . I have written following code . The service -- public…
0
votes
1 answer

Dependency between different concrete strategies in Strategy Design Pattern?

I'm implementing 2 different concrete strategies using the Strategy Design Pattern, and the second heavily reuses code from the first, while expending on it. I am wondering whether this "breaks" the advantages of the strategy design pattern, or…
0
votes
1 answer

Creating a pluggable StrategyFactory with DI

I have an engine that creates strategies for text file processing. These files are in various formats and the engine selects an appropriate strategy by passing the file through a chain of strategies until one of them asserts it can parse it. This…
0
votes
1 answer

observe data and select context strategy, depending upon the data values

Here follows a very basic Java version of Mudlet, a MUD client using Apache TelnetClient (not ssh). Largely as an exercise, Context has, so far, just the one strategy, the TargetStrategy, which returns a Deque dependent upon the…
Thufir
  • 8,216
  • 28
  • 125
  • 273
0
votes
1 answer

Strategy pattern for modifying internals of the caller?

Perhaps strategy pattern isn't what I'm after. Say my code looks like this (pseudo version): class Machine { private Stack _internals; public void DoOperation(Thingy x) { switch (x.operation) { case…
Tesserex
  • 17,166
  • 5
  • 66
  • 106
0
votes
1 answer

Do you still have the strategy pattern without the OCP?

I had an exam today on the subject of design patterns. There was one question I didn't know the answer to and I'm very curious. True or false "The open/closed principle is the key to the strategy pattern". I asked the professor what he meant by…
David
  • 965
  • 3
  • 12
  • 24
0
votes
1 answer

Strategy pattern with Factory class Unit testing

I was looking for some sample code for unit testing the strategy pattern method invocation. I have a strategy pattern class LeaveCalculator and based on the leave type the factory class will instantiate the specific calculator. For the Unit Test…
Saanch
  • 1,814
  • 1
  • 24
  • 38
0
votes
1 answer

Java Multiplayergame over Socket - use of Strategy pattern?

Since last week iam trying building an multiplayer game. iam statring with a little tictactoe game. I made a simple socket connection between server and client. On the server side iam making 2 thread for each connection. - readThread and…
wot-stefan
  • 91
  • 7
0
votes
1 answer

Method that relies on nested switch refactoring to Strategy pattern

I'm having a problem implementing a strategy pattern for a specific problem i'm encountering. I basically have a method makes a comparison between values. Apart from the two values, this method relies on two additional parameters to determine which…
Sem
  • 146
  • 9
0
votes
1 answer

2 classes implement same interface and one derives from another

I have an interface: Interface I { void Update(); } and 2 classes that implement it, A & B. Also both of the classes use the same exact function in update but B uses different code later on: Public class A : I { public void Update() { …
Greg Oks
  • 2,700
  • 4
  • 35
  • 41
0
votes
2 answers

C# strategy and observer pattern code

Hi I'm trying to create abstract class Person and two child classes Student and Staff. Person class also has an observer that is notified by a Practical class. But Students will be notified only about question number changed and Staff about…
user1926837
0
votes
0 answers

Is this right usage of Strategy pattern

I have a complex Network represented as hypergraph Hypergraph vertices are from various type:image,tag .... I have also various type of hyper-edge that relate vertices Homogenous HyperEdge :ImageImageHyperEdge ,TagTagHyperHedge ... Heterogenous…
nawara
  • 1,157
  • 3
  • 24
  • 49