Questions tagged [strategy-pattern]

-1

votes
0

answer
31

Views

JSON validation on Java

I want to make validations on the backend side, for a JSON structure received from the client. JSON example from frontend: { 'status': { 'type': 'string', 'value': 'test' }, 'validFrom': { 'type': 'number', 'value': '123123123' }, 'customerData': { 'type': 'map', 'value': { 'customerName': { 'type':...
Alex
1

votes
1

answer
54

Views

C++ Why emplacing object in vector segfaults?

I want to create a vector of 'Act' objects that contain pointers to either 'Eat' or 'Drink' dynamically allocated objects. The new objects are being emplaced like so: action_vector.emplace_back(Act::BehaviorType::eat); However, it is seg-faulting and I can't figure out why. I thought that emplace_ba...
user2520385
1

votes
0

answer
337

Views

strategy pattern with dictionary lookup

I'm writing a validation engine. I'm given with an a object payload containing around list of ~40 different properties. Every property will undergo different validation. The validations include checking if the field is a string and validating if the length exceeds permissible limit set by db and the...
Karthik Chintala
1

votes
0

answer
107

Views

Strategy Pattern with versions - UML Design

Currently I'm working on a webshop implementation in PHP (Aimeos, Laravel). I've arrived at the point where I need to create an import script. I started designing the UML class diagram and now I'm a bit stuck. The import script that will be running will have a lot of possible implementations. There...
qvotaxon
1

votes
2

answer
54

Views

How to elegantly change the program's behavior at runtime?

I am writing an Android game in Kotlin where the board changes according to a specific pattern - which pattern, depends on the level the user currently plays. I need a way to use many different patterns (20, 30 at max) in my code, deciding which one of them to use at runtime. I thought of encodi...
Neo
1

votes
0

answer
40

Views

Visitor pattern accepting a strategy - is there any gain?

I have been using the Visitor pattern for a payment system in my application. I have 10 payment types (paypal, banktransfer, stripe, omise, etc). but once i made it i was wondering the point of visitor pattern when i could use a strategy pattern. Let me show you what i have created so far: class Pa...
j2emanue
1

votes
1

answer
82

Views

How to use strategy pattern with custom sorting strategies?

I would like to know if the approach that I am using is the best or not for this situation. I'm developing a REST API and I have an endpoint which has to return a list of person objects. Let's say that person class has age (int), hair and vegan (both booleans), for example. That endpoint should ret...
Gerardo
1

votes
0

answer
12

Views

How to extract numeric value from a star-based rating system (works as string)

If anyone has a better title, please edit so and remove this line. Quick question: I have some product with a 1 to 5 star-based rating. The rating is currently loading in via a JSON file and is as followed: 'Rating': '5 stars'. I just want to extract the number from that string, even if that string...
Sander Koldenhof
1

votes
0

answer
55

Views

Using the Strategy Design Pattern with Python

I work at a place that has multiple projects. Depending on the project, I want items in a list processed in a certain way. I thought this problem would lend itself to the Strategy pattern. However, before committing fully to this pattern, I created a small app to demo how I think the final app wo...
Christopher Spears
1

votes
0

answer
38

Views

Does the strategy pattern incur more overhead than a switch statement?

I have a switch statement that currently handles around 20 different numerical event types. The event ID ranges from 1000-5000. The switch statement is becoming more difficult to maintain and read. My idea is to put each event type into its own class and then store it into an EnumMap. This approach...
user0000001
1

votes
0

answer
45

Views

Strategy pattern in Python : how to deal with value from one strategy to another?

I need your help to understand how to use Strategy pattern properly in Python. Indeed i don't know how to transfer a result coming from a strategy to another. Structure is : (files are at the bottom of this post) App.py AStrategyAbstract.py BStrategyAbstract.py CStrategyAbstract.py Running the app...
Tanc
1

votes
3

answer
186

Views

strategy pattern implementation with differing return classes

I have a MessageProcessor class which processes xml messages of different types. A switch statement (C#) based on the message type calls the appropriate method to parse the xml and extract the data required for the type of message. I would rather have a number of parser classes, one of which will b...
user158363
1

votes
1

answer
834

Views

Strategy Pattern replacing if/else or case statments

Why does it seem to me that using Strategy is just putting off the if/else to the Factory? Using Strategy, doesn't a Factory need to figure out which concrete class to instantiate, and doesn't it do so by if/else? Is another option to use a Map/List somehow, and have the keys be a name of the class...
bmw0128
1

votes
1

answer
258

Views

How to use external classes into a strategy pattern?

I implemented a strategy pattern for a poker hand history file parser (PHP or Java code). I have a problem to create a clean OOP way for classes and objects used by the several strategies. In real life : I have a class Game which has Hand(s) class Hand { ... // attributes for all strategies ... //...
Antoine
1

votes
1

answer
850

Views

How to dynamically load strategies (Strategy Pattern)?

My application will have different strategies for my objects. What is the best way to implement that? Ideally, I would like to dynamically load the strategy implementations from, say, some relational database. I'm not sure how to do that, though. What's the best approach? For instance, say that we w...
alexeypro
1

votes
1

answer
634

Views

Strategy Pattern Implementation

Following is the structure of my class public abstract class BaseUser { protected List permissions; public abstract void AddPerm(Perm perm); } public class NormalUser : BaseUser { public override void AddPerm(Perm perm) { throw new InvalidOperationException('A normal user cant add permissions'); } }...
asolvent
1

votes
1

answer
496

Views

optimising php validator to OOP

I need a php validator class that validates user inputs. I want it to be able to accept an assoc array of fields => values like: array( 'username' => 'Alex', 'email_address' => '@@#3423£[email protected]' ); and then return an array of errors like this: array( 'username' => '', 'email_address' => 'I...
AlexMorley-Finch
1

votes
2

answer
405

Views

Extend the strategy pattern: Add Context methods and subclasses

The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Very nice indeed. But consider this situation: I have a strategy pattern with a few subclasses of context and a few alg...
richn
1

votes
1

answer
407

Views

Do I use the Strategy or Command pattern for my case?

I have two algorithms that I'm implementing: AlgorithmA which works with Vector values and AlgorithmB which works with Matrix values What the algorithms have in common: Both are 'summation' algorithms that are supplied with the same sequence of inputs. Algorithms vary slightly on whether to accoun...
alhazen
1

votes
2

answer
458

Views

parameterized strategy pattern in MEF

I have three types of algorithms, each take a different parameter type needed for prcoessing. class AlgorithmA { Execute(int param); } class AlgorithmB { Execute(string param); } class AlgorithmC { Execute(); } to employ the strategy pattern with MEF, I did the following: interface IAlgorithm { int...
Salma Nafady
1

votes
3

answer
1.4k

Views

why we implement interface to abstract class

Pls find the link for reference: 'Strategy for success' article of JavaWorld My question is why do we need to have separate interface and implement it in abstract class, when we can declare those abstract methods in abstract class itself? ex in image, public interface Border(){ paintBorder(); getBor...
Sudhakar Reddy
1

votes
1

answer
223

Views

Difference between Strategy Pattern and State Pattern using example

Although there are many questions over the same topic, none of them cleared my confusion completely. So I am attempting to put an example and its solution with both patterns. Please tell me if I am doing it correctly or what wrong I am doing. Classes: BeforeCondition: defines voltage level, fuel qua...
1

votes
1

answer
314

Views

Is it better to use multiple SignalR Hubs or setup a strategy pattern

From my understanding of the SignalR documentation, you can have 1..* hubs, but only one pipe is created b/t client/server. I assume multiple hubs is good for organization, so which is best - multiple hubs, or setup a strategy pattern with your main hub connection class that manages tasks (OnConnect...
ElHaix
1

votes
3

answer
100

Views

Strategy pattern modification

How to modify classic strategy pattern in case when each ConcreteStrategy talks to DataStorage? There are different types of data storage and the interface depends on the concrete strategy.
typedef
1

votes
2

answer
1.3k

Views

Which design pattern for making reports in pdf, excel, etc [closed]

I have a school project where we must apply some design patterns in java code. I have to make a functionality where data is obtained by JPA/Hibernate and present it in a pdf or excel. I need to make it so that multiple file extensions can be added in the future. My question is, what design pattern...
Tim
1

votes
1

answer
351

Views

Understanding Strategy Pattern in PHP

I´m trying to understand the strategy pattern in PHP. My Example is based on this tutorials: http://www.d-mueller.de/blog/5-php-patterns-im-schnelldurchlauf-factory-iterator-observer-singleton-strategy/ For understanding I´ve cutted it a bit down: interface IStrategy { public function execute();...
t Book
1

votes
1

answer
335

Views

Applying the Strategy Pattern

Imagine you have a factory where products are carried through the manufacturing process by belts. Somewhere in the midst of this, it is necessary to split a stream of products from a single belt input out onto multiple belts based on some business logic. Perhaps they need to be split out by color...
Blackhawk
1

votes
2

answer
253

Views

Implementing strategy pattern when we need to modify the context class

I have a Genome class that contains an array of bits ('genes'), and I would like to implement different mutation methods to change the genes. I'm currently implementing the mutation method using a Strategy Pattern, but the mutation method changes the Genome class directly. Is this consider breaking...
Dan Tang
1

votes
3

answer
462

Views

Strategy Pattern - C++

I'm having an issue implementing a strategy pattern to my project. I created all the files necessary, however I am getting an error with my new call in main as I can't seem to initialize a strategy to what I want. Strategy.h /*All the classes that implement a concrete strategy should use this The A...
Vincent Fugnitto
1

votes
2

answer
130

Views

Strategy with dependency

So a have a class called Dictionary. And one of its' method is public boolean isRight(String word); which searches if the word is in the collection of words (that's encapsulated). private Set rightWords; So, I want to benchmark different types of searches that's why inside of this method I try to a...
Jax Lapusev
1

votes
1

answer
69

Views

Handling strategies with “complex” conditions without 'switch'/'if' statements

I am working on an application that will shortly be used for provisioning. The situation is not really complicated. The application manages groups that have access to services. Depending of the services the group has access, different business rules are applied. The first idea (simple and stupid) wo...
Ryuu
1

votes
1

answer
61

Views

How to refactor similar looking functions across 2 different class based views in Django 1.10?

Am using codecoverage and it complains that I have 2 functions in 2 different class based views that are too similar. Attached is the codecoverage error Below are the code that were highlighted: class PalletContentPickup(APIView): ''' Picking up a pallet content to transfer to exiting pallet content...
Kim Stacks
1

votes
1

answer
914

Views

Strategy Pattern using Unity

I'm trying to use the Strategy pattern using dependecy injection with Unity, and i have the following scenario: public Interface IName { string WhatIsYourName() } public class John : IName { public string WhatIsYourName() { return 'John'; } } public class Larry : IName { public string WhatIsYourNam...
Mario Guadanhim
1

votes
1

answer
53

Views

Implementing strategy pattern in XQuery, reflection functions in general

I wonder if there is a way to introspect XQuery modules and dynamically access functions. This would help me to implement the GOF strategy pattern as XQuery does not have support for multiple implementations for the same interface. The problem, of course, is that there seems to be no support for i...
Kemal Erdogan
1

votes
1

answer
80

Views

How to be sure that your code follows the SOLID principle?

I have a doubt about a Object Oriented design. Let's say I have to implement a generic bill evaluater for phone calls. In input I can use a log of calls (number and duration in seconds) and I want get the price for each call. What I thought is to use a generic Call class and an abstract BillEvaluat...
gepeppe
1

votes
1

answer
146

Views

Strategy pattern and two repositories

I have a strategy pattern in my business logic, which is referenced to two application. One is a Web MVC application and the other one is a Xamarin App. Now I want to write / read data from the database in this strategy pattern, but I have two different repository. How can I inject the right one int...
user1861065
1

votes
1

answer
64

Views

Passing large objects containing data into an interface

Context I have an abstract class that is using the strategy pattern. public interface IReader { HashSet Read(string fullFilePath, HashSet sheetNames, ref string errors); } public abstract class AbstractReader : IReader { public virtual HashSet Read(string fullFilePath, Hashset sheetNames, ref string...
Eddy
1

votes
1

answer
98

Views

Multiple Strategies in Strategy Pattern Context

I have a question about the strategy pattern. Usually the Strategy Pattern looks like this: class TaxCalculatorContext { private $strategy; public function setStrategy(TaxCalculatorStrategyInterface $strategy) { $this->strategy = $strategy; } public function execute($amount) { return $this->strategy...
coder_1432
1

votes
2

answer
92

Views

Strategy pattern in Objective-C

I wrote a sample code strategy design pattern. @protocol MyProtocol - (void)execute1; @end @interface BaseClass : NSObject @property(nonatomic, assign) NSInteger commonValue; - (void)commonCalculator; @end @interface DogClass : BaseClass @end @interface CatClass : BaseClass @end In addition, I wan...
user3569109
1

votes
1

answer
41

Views

Can I use lamba experssion to obtain an ejb dynamically through strategy pattern with CDI?

I know I can inject as an instance all the beans that match the interface and then choose between them programmatically : @Inject @Any Instance paymentProcessorSource; That means I have to put the selecting logic into the client. Can I, as an alternative, cache the value of the ejb using lexical sco...
giovanni esposito

View additional questions