strategy-pattern

C#: Abstract Strategy base class serving as Abstract Factory for Strategy objects

↘锁芯ラ 提交于 2019-12-09 19:04:59
问题 I am trying to create a web-based tool for my company that, in essence, uses geographic input to produce tabular results. Currently, three different business areas use my tool and receive three different kinds of output. Luckily, all of the outputs are based on the same idea of Master Table - Child Table, and they even share a common Master Table. Unfortunately, in each case the related rows of the Child Table contain vastly different data. Because this is the only point of contention I

Strategy pattern using Enums. Need a simple example in Java [duplicate]

白昼怎懂夜的黑 提交于 2019-12-09 01:21:27
问题 This question already has answers here : Where is the benefit in using the Strategy Pattern? (8 answers) Closed 5 years ago . I'm trying to understand the strategy pattern and enums in java. I've googled this and have found a few articles on the subject, but most of them seemed too complex for my understanding. Could anyone provide a simple example or another link that demonstrates strategy pattern using enums in laymen terms using java? Thanks you in advance. 回答1: This should do: interface

Java equivalent of .NET Action<T> and Func<T,U>, etc [duplicate]

谁说胖子不能爱 提交于 2019-12-08 15:09:22
问题 This question already has answers here : Java's equivalents of Func and Action (8 answers) Closed 6 years ago . Are there any standard generic "callback" or "function/method" types in Java, like System.Action<T> or System.Func<T,U> in .NET? In my concrete case, I need a class that wraps a method that takes one (generic) parameter of type T and returns nothing (i.e. void ). Yes, it's easy enough to create such a class/interface for myself, but I'd prefer a standard library class if there is

How to refactor code to avoid multiple if-s [from interview]?

限于喜欢 提交于 2019-12-08 12:20:45
问题 On interview I was asked the following question: I have following method: public void foo(SomeObject o){ if(o.matches(constant1)){ doSomething1(); }else if(o.matches(constant2)){ doSomething2(); }else if(o.matches(constant3)){ doSomething3(); } .... } question was: you should refactor method above. What will you do? On interview I didn't grasp how to make it. Now I think that state design pattern is suitable for this task ? Am I right? What do you think? P.S. I negotiated with my colleague.

DI, Guice and Strategy Pattern

白昼怎懂夜的黑 提交于 2019-12-08 10:56:03
问题 Suppose I have the following base class, Queen and Knight as its derivatives. WeaponBehaviour is an interface. I can't figure out how to inject weapons using Guice depending on the concrete GameCharacter type. public abstract class GameCharacter { @Inject protected WeaponBehaviour weapon; public GameCharacter() { } public void fight() { weapon.useWeapon(); } public void setWeapon(WeaponBehaviour weapon) { this.weapon = weapon; } } 回答1: You could use Binding Annotations. A subclass: class

Strategy pattern files location Rails

柔情痞子 提交于 2019-12-08 06:13:42
问题 I have a couple of questions: following the perfect response that I got on that question Business logic dependent on model attribute. I plan on usin a Strategy pattern, and I was wondering where I (in which folder) should put the classes files? should they go in the models folder? Also, I have a default behavior, do I still need to create a Interface class and have my default strategy derived from this, or I define my default strategy as the base strategy? 回答1: This is a subjective answer,

Strategy Pattern in C++ with template

喜你入骨 提交于 2019-12-08 02:24:04
问题 This is a sample implementation of Strategy Pattern in C++: ConcreteStrategy.h class ConcreteStrategy { public: ConcreteStrategy(); ~ConcreteStrategy(); const OtherObject* doSomething(const OtherObject &obj); }; ConcreteStrategy.cpp #include "ConcreteStrategy.h" ConcreteStrategy::ConcreteStrategy() { // etc. } ConcreteStrategy::~ConcreteStrategy() { // etc. } const OtherObject* ConcreteStrategy::doSomething(const OtherObject &obj) { // etc. } MyContext.h template <class Strategy> class

Design pattern for cost calculator app?

橙三吉。 提交于 2019-12-07 16:59:32
问题 I have a problem that I’ve tried to get help for before, but I wasn’t able to solve it then, so I’m trying to simplify the problem now to see if I can get some more concrete help with this because it is driving me crazy… Basically, I have a working (more complex) version of this application, which is a project cost calculator. But because I am at the same time trying to learn to design my applications better, I would like some input on how I could improve this design. Basically the main thing

Should safe pointers be used in strategy pattern?

回眸只為那壹抹淺笑 提交于 2019-12-07 12:53:41
问题 Given a typical strategy pattern class Strategy { public: virtual int execute() const = 0; } class StrategyA : public Strategy { public: int execute() const override; } class StrategyB : public Strategy { public: int execute() const override; } I believe the 'pre-C++11' way to implement a context class would be something like class ContextRaw { public: ContextRaw(Strategy* the_strategy); ~ContextRaw(); // Should this delete the_strategy_? int execute() const; private: Strategy* the_strategy_;

Avoiding coupling with Strategy pattern

ⅰ亾dé卋堺 提交于 2019-12-07 05:47:40
问题 I am attempting to apply the Strategy pattern to a particular situation, but am having an issue with how to avoid coupling each concrete strategy to the context object providing data for it. The following is a simplified case of a pattern that occurs a few different ways, but should be handled in a similar way. We have an object Acquisition that provides data relevant to a specific frame of time - basically a bunch of external data collected using different pieces of hardware. It's already