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

Trying to understand the wikipedia strategy pattern example using new Func

I was looking at this, http://en.wikipedia.org/wiki/Strategy_pattern and I understand the concept of the strategy pattern, but could someone explain the C# example a bit. I dont really get the how and why of the definition of 'Strategy' in the…
7
votes
3 answers

Inversion of Control, Dependency Injection and Strategy Pattern with examples in java

I am often confused by these three terms. These three look similar to me. Can someone please explain them to me clearly, with examples. I have seen similar posts and don't understand completely.
7
votes
4 answers

Design Patterns - Strategy Pattern

I am a beginner in Design Patterns. Suppose I am developing a C# application to track the development works performed by various members in development team (i.e. a Project Tracker). I am trying to be inspired by Strategy Pattern. So I am designing…
user366312
  • 16,949
  • 65
  • 235
  • 452
6
votes
2 answers

Is there a built-in Java type that guarantees an execute(T t) method?

It seems the need for a type like the following would be so ubiquitous that something like it should be already built into Java: public interface Executer { void execute(T object); } It can then be used in other classes like this trivial…
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
6
votes
2 answers

Emailer in Java using Strategy Pattern

UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom emailing utility. Now, to obey Single Responsibility Principle, I want to have the following classes: MailerSender, MailProvider and EmailObject. The MailSender…
djunforgetable
  • 859
  • 3
  • 9
  • 16
6
votes
2 answers

How to reduce number if-statements using dict?

I have the following code with multiple cases: def _extract_property_value(selector: Selector) -> str: raw_value = selector.xpath("span[2]") default_value = raw_value.xpath("./text()").get().strip() value_with_a = ',…
Karany
  • 63
  • 3
6
votes
2 answers

Best way to validate a String against many patterns

This is a question more about best practices/design patterns than regexps. In short I have 3 values: from, to and the value I want to change. From has to match one of several patterns: XX.X >XX.X >=XX.X
Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
6
votes
9 answers

What is the exact definition of the strategy design pattern?

I had a geek fight with someone over what the strategy pattern really is and I need a expert to settle the matter. We both agree that the strategy pattern allows for the guts of a class (e.g., the behavior) to be swapped out at runtime while…
Dave
  • 4,050
  • 6
  • 30
  • 35
6
votes
2 answers

Modifying if-else to strategy pattern

I have the following if-else branch in java. if (str.equals("a")) { A;} else if (str.equals("b")) { B;} else if (str.equals("c")) { C;} else if (str.length == 5) { D;} else { E;} how to modify this code into strategy pattern ?
sam
  • 651
  • 3
  • 9
  • 16
6
votes
4 answers

C# strategy design pattern for different return types

I try to apply the strategy design pattern for parsing of some textual content where each result is represented in different class. Minimal example. So my interface looks like this: public interface IParseStrategy { object Parse(string…
PythonNoob
  • 914
  • 1
  • 7
  • 15
6
votes
3 answers

Mechanism for Dependency Injection to Provide the Most Specific Implementation of a Generic Service Interface

I feel like I played buzzword bingo with the title. Here's a concise example of what I'm asking. Let's say I have some inheritance hierarchy for some entities. class BaseEntity { ... } class ChildAEntity : BaseEntity { ... } class GrandChildAEntity…
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
6
votes
3 answers

What is the diffrence between strategy design pattern and abstract factory pattern?

Can someone once and for all explain to me the difference between these two and try to give a sort of guideline for when to use each one of them? Examples would be really nice.
RanZilber
  • 1,840
  • 4
  • 31
  • 42
6
votes
3 answers

Strategy or Adapter pattern?

I want to create some classes of each Virtual Server Provider, for example: Digital Ocean Linode Amazon AWS Each Provider has own PHP class (via composer) to use their API interface, I want to use their class library but I want to make sure I can…
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
6
votes
2 answers

Method refactoring?

In the TokenRepository you can see 3 similar methods. It create new entry to the tokens table but each method has different fields. How can I refactor this? Should I merge 3 methods into 1 method or should I use strategy pattern? TokenRepository…
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
6
votes
4 answers

Confused about strategy design pattern

I can not understand why to use Context module(which we will see in the following codes) in strategy design pattern, what its function? Let's see one part of the strategy design pattern. public interface Strategy { public int doOperation(int…
Ivan
  • 661
  • 1
  • 8
  • 15