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

Strategy pattern: coupled strategy vs uncoupled strategy

What´s the difference between a coupled strategy and an uncoupled strategy? Thanks for answering.
MQSJ23
  • 107
  • 2
  • 2
  • 6
4
votes
4 answers

How To Have A Single Interface Return Different Data Types?

In a nutshell, I want to use a single interface, IProducer, to create an object, IProduct. IProduct will have different components depending on which interface created it. The IProduct class will then be used by the IConsumer interface. The correct…
trianta2
  • 3,952
  • 5
  • 36
  • 52
4
votes
2 answers

Difference between State pattern and Strategy pattern

Looking at the GoF patterns I find the similarities between State and Stategy pattern rather striking. Both swap out polymorphic classes to modify behavior. Anyone else found the same? What are the exact differences?
user2657232
4
votes
2 answers

When is it appropriate to create a Decorator for an object, and when is it appropriate to rewrite your object to allow Strategies to be applied?

For example, suppose I have a Product class that I can add to a shopping cart. I may want to be able to package it together with another item when it is also in the cart and add a 15% discount. Should the product class be decorated with a new…
Zak
  • 24,947
  • 11
  • 38
  • 68
4
votes
4 answers

C++ Strategy Design Pattern, making an interface array

After having implemented the strategy pattern, I wanted to make an array of the interface-type, to which I can then add any concrete type. For those who don't know the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern In this…
user23163
  • 515
  • 2
  • 9
  • 15
4
votes
1 answer

UML Class Diagram for Strategy Pattern

I have an exam in a couple of hours and I am trying to solve sample questions for it. I couldn't find a solution to following question. Draw a class diagram corresponding to the following situation: A grocery store (e.g., a supermarket) sells…
hevele
  • 903
  • 6
  • 20
  • 38
4
votes
2 answers

Decorate a char* and char const* by pointer acquisition : good practice?

Hello I wanted to poll the public about my idea of doing a string class (like std::string) that would have a feature of being able to work on buffers provided by the client. What are the dangers that you forsee ? is it a classic smell ? etcaetera I…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
4
votes
4 answers

Interface Hell or Acceptable Design?

I am refactoring some legacy code that was doing close to the same thing over and over via a case statement: switch(identifier) case firstIdentifier: (SomeCast).SetProperties(Prop1,Prop2,Prop3); break; ... case…
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
4
votes
5 answers

Strategy Design pattern vs State Design pattern

I was reading through this link about the state pattern. Is it looks like strategy pattern? What is the exact difference between these two patterns?
Krishna
  • 7,154
  • 16
  • 68
  • 80
4
votes
1 answer

Is this a good use case of the Strategy Pattern?

I'm working with an application that needs to receive several videos, and display them on a particular page, currently these videos can only be from YouTube, due to an implementation that does not allow other providers, because the code to get the…
3
votes
1 answer

How to model this in DDD and repository pattern

I want to model service like this public class FooService { public GetById(ISecurityContext context, id) { //checking context has right to view //calling Foo repository to getById } public Add(ISecurityContext…
3
votes
1 answer

Is this a right Strategy implementation

I have tried to implement the Head First Duck problem with Startegy. I am trying to implement the Decoy Duck, which intially don't have the facility to Quack or Fly is implemented by calling a a default constructor( I know this duck don't have the…
Tutu
  • 33
  • 1
  • 5
3
votes
4 answers

php percentage chance

This is really more a question of approach, but I'm presenting it in php. Suppose we had a list of four percentages that a give event will occur on iteration. array=('walk the dog'=>.25,'read the paper'=>.25,'drink coffee'=>.0,'listen to…
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
3
votes
2 answers

Clean implementation of the strategy pattern in Perl

How do I write a clean implementation of the strategy pattern in Perl? I want to do it in a way that leverages Perl's features.
sutee
  • 12,568
  • 13
  • 49
  • 61
3
votes
2 answers

Is strategy pattern helpful for this scenario?

I have two classes A & B and both class implements the interface ISomeInterface. But some properties are not required for both class A & B. But in the client app I am calling the same ISomeInterface to invoke both the classes. The problem which I…
user972255
  • 1,828
  • 10
  • 32
  • 52