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

Implementing the Strategy Pattern against generated pojos

I am writing a parser for a couple of different DB tables. We're using Hibernate with Eclipse, and we've made hbm.xml mapping files that correspond to our tables so that our .java files are generated in line with the tables. We've added a new table…
Jimbob
  • 63
  • 1
  • 1
  • 4
0
votes
3 answers

Strategy Design Pattern (with argument method) - JAVA

I'm writing an algorithm which works on a bit serie and do some operations such as shift, AND with it. I wanted to test the algorithm with two different data structure : MutableBigInteger and BitString ( javabdd ), so I thought I'll try to come up…
Nikkolasg
  • 444
  • 4
  • 18
0
votes
1 answer

Trouble implementing strategy and composite pattern to give varying behaviour in a game

I'm running into real trouble trying to complete a practical that requires using strategy and composite pattern. I am trying to create a collection of vehicles which can have different behavior depending on the surface they are on. However, these…
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
0
votes
2 answers

Something it is wrong in implementation of Strategy Pattern in c#

I am trying implement a Strategy pattern with nested classes. public class Restriction { protected SpecificRestriction _specificRestriction; public void SetGreaterRestriction(decimal value) { Greater greaterRestriction = new…
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90
0
votes
2 answers

C++ passing an unknown type to an object which has an abstract base

I have another question following on from my previous question about abstractions, I have another problem in relation to setting data at the event once it has been created. As things stand, I can create events of any type and apply their data no…
Agenten
  • 23
  • 2
0
votes
2 answers

Difference between Vistor pattern and Stategy pattern?

I read about visitor pattern at http://en.wikipedia.org/wiki/Visitor_pattern Initial understanding of this pattern created impression that visitor pattern is same as Bridge/Strategy pattern. So is that specific example creating such impression ?…
Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76
0
votes
2 answers

Strategy pattern and iterator on all algorithms

I have a list of algorithms that I want to run on an image for denoise it. I have several different algorithms for denoising my pics. Say I have denoise_1 and denoise_2 and denoise_3. But in the future I will need to add denoise_4,…
user1993478
  • 55
  • 1
  • 4
0
votes
1 answer

How to test a Factory / Strategy implementation with PHPUnit

I have a Factory class that returns a writer strategy based on the extension of a given file: public static function getWriterForFile($file) { // create file info object $fileInfo = new \SplFileInfo($file); // check that an extension is…
shanethehat
  • 15,460
  • 11
  • 57
  • 87
0
votes
2 answers

Does the compile warning point to bad design?

Possible Duplicate: How to suppress Java compiler warnings for specific functions I would like to use a Map to implement the Strategy pattern. However, I cannot find a way of doing this in a way that plays nicely with generics. Is it possible to…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
0
votes
3 answers

Do I have to compose every method in my main class in order to use it (Strategy pattern)?

I've been using this tutorial to look into the Strategy pattern. I receive the output he talks about, but it seems like there is no option to use the digHole() method. When I call the method in the Dog() constructor it works though. My guess is this…
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0
votes
1 answer

How to map classes in Hibernate that only have behavior?

i'm mapping some classes in hibernate, these classes are basically a strategy pattern. I have a class "User" that knows an abstract class "UserType" that declares an abstract method "purchaseSomething()", the subclasses override this method. For…
4gus71n
  • 3,717
  • 3
  • 39
  • 66
0
votes
2 answers

strategy design pattern

Hi everyone I am trying to implement the strategy pattern but I can not set the amount in the concrete classes, what i mean is that the amount is remianing the same as the one in the helper class that has a relationship with the interface. I tried…
Doesn't Matter
  • 405
  • 4
  • 12
  • 23
0
votes
2 answers

Is it possible to use a Strategy pattern for data structure with no common ancestor?

I have two data structure classes (this is a simplified version of my code) Animal: Has one property “int Age” Person: Has one property “DateTime Birthday” What I am trying to accomplish is to compile “Uploading” (persisting to database), which…
dance2die
  • 35,807
  • 39
  • 131
  • 194
0
votes
3 answers

Iterating through strategy design pattern

I have a list of algorithms that I want to run on a dataset. For example, say my dataset is a list of addresses. I need to check the validity of the addresses but I have several different algorithms for validating. Say I have validation_one and…
Brand
  • 1,681
  • 1
  • 24
  • 32
0
votes
3 answers

Different algorithm for different inputs

In the application I am developing I am facing a situation; I want to know if there is a design pattern for this. It's as follows User is presented on a web interface with different algorithms for a process User selection is stored in the database.…
satyajit
  • 1,470
  • 3
  • 22
  • 43