Questions tagged [strategy-pattern]

26 questions
21
votes
6 answers

Strategy vs Factory design pattern

I am new to design patterns and working my way through the Factory Method and Strategy patterns. I understand that Factory is a creational pattern and Strategy is behavioral but I struggle to understand when to use which one. For example, I have the…
10
votes
5 answers

Can we completely replace inheritance using strategy pattern and dependency injection?

For example: var duckBehaviors = new Duckbehavior(); duckBehaviors.quackBehavior = new Quack(); duckBehaviors.flyBehavior = new FlyWithWings(); Duck mallardDuck = new Duck(DuckTypes.MallardDuck, duckBehaviors) As the Duck class contains all the…
3
votes
4 answers

Is the strategy pattern for 3 variants overuse?

I am curious about it because I talked with a friend about the strategy pattern, and we diverge about when we have to use it. Our scenario is that I have to build a view component with 3 variants. All variants have particular colors and ways to…
ViTUu
  • 139
  • 2
3
votes
3 answers

How to use strategy pattern more effectively?

The problem is the following: I have to download a set of JSON files and convert them to a certain format. There are 5 output formats (Let's call them A, B, C, D, E) and all of the downloaded json files are going to be in of of two formats (call…
3
votes
2 answers

DDD - delegate business rule of domain object to external service

Situation: I am implementing DDD in my first project and I would like to clarify how to correctly implement strategy pattern (in my case check if API token is active). This is a business rule of ApiToken instance but there could be various…
2
votes
1 answer

The notion of configurable strategies

I'm designing an algorithm that matches entries based on some notion of "proximity" (for the sake of discussion, assume we're matching floats). Furthermore: The input is a scalar and a vector, and the output should the "closest" element of the…
2
votes
1 answer

Is it allowed to include the composition in the compositor in the Strategy Pattern

I have a range of different animals in my zoo such as turtles, birds etc. As they all share a common trait such as either swimming, flying etc., I thought a strategy pattern would be appropriate to model them. The thing is though that I want to call…
Tyler D
  • 129
  • 2
2
votes
2 answers

Strategy pattern and different implementation

I am still learning Design Patterns and I have a situation in my hand. I am developing a simple employee management Python program. An employee can be a Developer, Senior Developer, Lead Developer, Manager. Now during the tenure employee may get…
2
votes
2 answers

Strategy/domain object responsibilities

Assume we are in the world of car rental application :) Let's say that I have 3 types of cars with 2 categories of price: Car type Price category ------------------------------ Sport High Luxury High Economy …
2
votes
2 answers

How to implement Strategy pattern for combined behaviours

Strategy pattern solves the necessity of applying a certain algorithm/behaviour depending on the object's type itself. So you can iterate over a bunch of similar objects and call the same function expecting different algorithms to be executed for…
2
votes
1 answer

Does my use of the strategy pattern violate the fundamental MVC pattern in iOS?

I'm about to use the 'strategy' pattern in my iOS app, but feel like my approach violates the somehow fundamental MVC pattern. My app is displaying visual "stories", and a Story consists (i.e. has @properties) of one Photo and one or more…
Goodsquirrel
  • 123
  • 5
1
vote
0 answers

Is it bad practice to specialize template method to implement strategy pattern in C++?

I came across a class that implements a kind of "strategy pattern" with a concrete implementation defined inside the main class as a template method. #include struct ObjA { int a = 123; }; struct ObjB { int b = 456; }; class…
Delgan
  • 366
  • 2
  • 13
1
vote
1 answer

Self-Selecting Variant of the Strategy Pattern?

I've found this pattern useful, and am trying to classify or name it. Basically, that: A task should be performed by different strategies, depending on the context. Each concrete strategy implements a common interface. BUT... caller doesn't choose…
MrTrick
  • 113
  • 3
1
vote
2 answers

Difference between Strategy pattern and Repository pattern

I found the following definition of Repository Pattern: Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and…
1
vote
3 answers

Advantages of strategy design pattern versus simple if-else

I am not clear on what advantage does strategy pattern offers over simple if-else. Example of poor code //poor code that we all can agree is not the way to go ----- main class ----- String strategyname; //lets assume at this point variable…
1
2