strategy-pattern

Strategy Pattern vs Dependency Injection

岁酱吖の 提交于 2019-12-06 18:38:21
问题 How is strategy pattern is different then dependency injection? ie below is what you can do with Strategy pattern: class Foo{ private readonly ISortAlgo _sortAlgo; public Foo(ISortAlgo sortAlgo) { _sortAlgo = sortAlgo; } public void Sort() { _sortAlgo.sort(); } } with DI you can do the same, essentially you can have constructor, setter, interface etc. injection. and it would give the same effect as Strategy pattern. I am aware that DI is also set of other principles, such as loose coupling,

Strategy pattern files location Rails

筅森魡賤 提交于 2019-12-06 16:44:34
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? This is a subjective answer, therefore it's likely it will be closed at some point. As I provided an answer to the original question, let

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

♀尐吖头ヾ 提交于 2019-12-06 02:54:49
问题 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 : ChildAEntity { ... } class ChildBEntity : BaseEntity { ... } Now let's say I have a generic interface for a service with a method that uses the base class: interface IEntityService<T> where T : BaseEntity { void DoSomething(BaseEntity entity)...

Strategy pattern in F#

岁酱吖の 提交于 2019-12-06 01:40:04
问题 In C# I have the following code: public class SomeKindaWorker { public double Work(Strategy strat) { int i = 4; // some code ... var s = strat.Step1(i); // some more code ... var d = strat.Step2(s); // yet more code ... return d; } } This is a piece of code that can do some kind of work by using a provided strategy object to fill in parts of the implementation. Note: in general the strategy objects do not contain state; they merely polymorphically provide implementations of individual steps.

How can a delegate respond to multiple events with a generic and extensible class?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 01:29:02
问题 I have rigged up a technique to handle multiple subreports in an rdlc report, but as I have tried to make it generic and repeatable, I have instead had to take the model and tweak it slightly for each case. For example, if I define an abstract interface, like such, I just cut and paste it from winform to winform as needed: abstract class ISolutionStrategy { public abstract void AlgorithmInterface(Int64 searchCriteria, SubreportProcessingEventArgs e); } First, I want to be able to bring this

Design pattern for cost calculator app?

霸气de小男生 提交于 2019-12-05 19:54:49
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 I want is input on the conditionals that (here) appear repeated in two places. The suggestions I got

Avoiding coupling with Strategy pattern

孤者浪人 提交于 2019-12-05 09:03:45
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 too large because of the amount of data it contains, so I don't want to give it any further

Replacing if/else logic with state/strategy pattern

故事扮演 提交于 2019-12-05 05:53:52
I have read the previous stack exchanges on replacing conditional logic in Java such as IF/ELSE with State/Strategy patterns but I am not sure whether my case is a proper fit for the replacement. Here are coupled I looked at - Long list of if statements in Java and Converting many 'if else' statements to a cleaner approach I am essentially writing a file download manager and these are my IF/ELSE constructs: If the file and it's zip file exist then move zip file to zip file directory and read in file If the zip file exists then unzip file and move zip file to zip file directory and read in file

Fetching Strategy example in repository pattern with pure POCO Entity framework

百般思念 提交于 2019-12-04 19:24:21
I'm trying to roll out a strategy pattern with entity framework and the repository pattern using a simple example such as User and Post in which a user has many posts. From this answer here , I have the following domain: public interface IUser { public Guid UserId { get; set; } public string UserName { get; set; } public IEnumerable<Post> Posts { get; set; } } Add interfaces to support the roles in which you will use the user. public interface IAddPostsToUser : IUser { public void AddPost(Post post); } Now my repository looks like this: public interface IUserRepository { User Get<TRole>(Guid

Strategy Design Pattern, Generics and TypeSafety

血红的双手。 提交于 2019-12-04 12:09:33
问题 I want to create the following Strategy Pattern combined with Factory, but I want it to be typesafe. I have done the following till now: public interface Parser<T> { public Collection<T> parse(ResultSet resultSet); } public class AParser implements Parser<String> { @Override public Collection<String> parse(ResultSet resultSet) { //perform parsing, get collection Collection<String> cl = performParsing(resultSet); //local private method return cl; } } public class ParserFactory { public enum