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
8
votes
2 answers

Composite Strategy pattern - java - How bad is this code?

This question is kind of continuation to my earlier post: Visitor pattern implementation in java- How does this look? I got a bit confused while refactoring my code. I am trying to convert my visitor pattern (explained in the prior post) into a…
Jay
  • 2,394
  • 11
  • 54
  • 98
8
votes
7 answers

Is this a decorator or a strategy pattern, or neither of the two?

I have the following interface. PowerSwitch.java public interface PowerSwitch { public boolean powerOn(); public boolean powerOff(); public boolean isPowerOn(); } The above interface should consist of the minimum set of methods which…
etxalpo
  • 1,146
  • 1
  • 14
  • 25
8
votes
5 answers

Design pattern: avoid switch to decide which service call

For a project, we have a Controller/Service/DAO architecture. We implement calls to different providers' APIs so we ended up with some boilerplate code like this in every controller class: enum { PARTNER_A, PARTNER_B, PARTNER_C } public class…
luso
  • 2,812
  • 6
  • 35
  • 50
8
votes
7 answers

Is 'Strategy Design Pattern' no more than the basic use of polymorphism?

In Strategy Design Pattern, what we do is Create a common interface. Implement a set of classes using that interface with overridden method(s). Let the run time to choose the actual class for an object which has the same type with that common…
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
8
votes
1 answer

Naming convention for common patterns?

Just as there is a naming convention for the Observer pattern (or rather, a naming convention for events in languages such as C#) using Event/Handler passing EventArgs and such, are there naming conventions that you use to easily highlight other…
bwerks
  • 8,651
  • 14
  • 68
  • 100
8
votes
2 answers

Replacing if/else logic with state/strategy pattern

I have read the previous stack exchanges on replacing conditional logic in Java such as IF/ELSE with State/Strategy patterns but I am not sure whether my case is a proper fit for the replacement. Here are coupled I looked at - Long list of if…
gansub
  • 1,164
  • 3
  • 20
  • 47
8
votes
1 answer

Singleton with the strategy pattern

Here's what the interface of a strategy could look like public interface Strategy { public void doStuff(Object o); } And here's a possible implementation public class StrategyImpl implements Strategy { @Override public void…
EFTH
  • 475
  • 5
  • 14
8
votes
3 answers

Strategy Design Pattern, Generics and TypeSafety

I want to create the following Strategy Pattern combined with Factory, but I want it to be typesafe. I have done the following till now: public interface Parser { public Collection parse(ResultSet resultSet); } public class AParser…
ChrisGeo
  • 3,807
  • 13
  • 54
  • 92
8
votes
1 answer

Using Factory with Strategy design pattern

Okay, so I have been given an assignment where I am asked to use both the Strategy and Factory design patterns. Here is the problem: You are developing an application for a bank to use for handling loans. There is a Loan class with a method…
Cory Gross
  • 36,833
  • 17
  • 68
  • 80
8
votes
0 answers

Head First Design Patterns - Strategy Pattern - Python

This is my first attempt at the Strategy Design Pattern. I have no architectural experience using Python, so constructive comments would be welcome. I'm posting this to Stack-Over-Flow because I would have found it useful as a sanity check this…
Dave Kirkby
  • 409
  • 1
  • 4
  • 14
7
votes
4 answers

How to implement usage of multiple strategies at runtime

I need to process a list of records returned from a service. However the processing algorithm for a record changes completely based on a certain field on the record. To implement this , I have defined an IProcessor interface which has just one…
Sennin
  • 1,001
  • 2
  • 10
  • 17
7
votes
1 answer

Application specific implementation of a class method

I have a library (add-in) with a class that is used in a few small applications. I want to provide a Save method to that class, which will depend on the application that is running. To solve it I am trying to use a strategy pattern(I might be…
Victor K
  • 1,049
  • 2
  • 10
  • 21
7
votes
6 answers

in c# what is the difference between strategy pattern and delegates?

I've been looking at strategy pattern implementation examples and it seems to me like they are very similar to c# delegates. The only difference I see is that strategy pattern implementations don't need to explicitly declare a delegate. But other…
ijdk
7
votes
2 answers

Can't call static method from class as variable name?

I'm using php 5.2.6. I have a strategy pattern, and the strategies have a static method. In the class that actually implements one of the strategies, it gets the name of the strategy class to instantiate. However, I wanted to call one of the static…
user151841
  • 17,377
  • 29
  • 109
  • 171
7
votes
3 answers

Combining Strategies in a Java Strategy Pattern

Examples below are shamelessly ripped off of java.dzone.com, and modified to suit my needs: Our interface: public interface CompressionStrategy { public void compressFiles(ArrayList files); } Our First Implementation public class…
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173