strategy-pattern

State monad and strategy pattern

元气小坏坏 提交于 2019-12-04 05:54:39
I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter . All it does, in its basic implementation, is to take a some datafeed of type 'd and update itself, generating a new updated copy of itself. [<AbstractClass>] type Filter<'d, 'F> (state: 'F) = member val StateVariable = state with get abstract member Update: 'd -> Filter<'d, 'F> I have then a ISignalGenerator , that takes a filter, environmental data and process it to generate a Signal of type 'S . type

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

跟風遠走 提交于 2019-12-04 05:40:02
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)... } I have some concrete implementations: class BaseEntityService : IEntityService<BaseEntity> { ... }

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

淺唱寂寞╮ 提交于 2019-12-04 05:29:59
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 into each form by including an has-a object. I also want to encapsulate the behaviors of handling the

How can I use a static method as a default parameter for the strategy design pattern?

匆匆过客 提交于 2019-12-04 03:24:31
问题 I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") def __init__(self, strategy=C.default_concrete_strategy): self.strategy = strategy def execute(self): self.strategy() This gives the error: NameError: name 'C' is not defined Replacing strategy=C.default_concrete_strategy with strategy=default_concrete_strategy will work but, left as

Does this Java Strategy pattern have a redundant Context class?

和自甴很熟 提交于 2019-12-03 23:54:50
The following code sample is an implementation of the Strategy pattern copied from Wikipedia . My full question follows it... The Wiki's main method: //StrategyExample test application class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrategy(3,4); context = new Context(new ConcreteStrategySubtract()); int resultB = context.executeStrategy(3,4); context = new Context(new ConcreteStrategyMultiply()); int resultC = context

What is the exact definition of the strategy design pattern?

折月煮酒 提交于 2019-12-03 15:49:26
I had a geek fight with someone over what the strategy pattern really is and I need a expert to settle the matter. We both agree that the strategy pattern allows for the guts of a class (e.g., the behavior) to be swapped out at runtime while maintaining the same interface. However, her contention is that "For [the algorithms] to be a strategy, You would have to get the same results". My contention is that swapping an "algorithm" or logic of a class could mean that the results of the overridden operation are different, but that it still meets the purpose, intent (and classification) of the

Unity Resolve Multiple Classes

末鹿安然 提交于 2019-12-03 13:22:22
How do I get microsoft unity to 'construct' a list of classes for a given interface type. Very Simple example: List<IShippingCalculation> list = new List<IShippingCalculation>(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List<IShippingCalculation> list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity, must i do this for all the concrete classes? //how does it know to give a list. Container.RegisterType<IShippingInfo

Polymorphic Enums for state handling

隐身守侯 提交于 2019-12-03 10:58:29
问题 how do i handle Enums without using switch or if statements in C#? For Example enum Pricemethod { Max, Min, Average } ... and i have a class Article public class Article { private List<Double> _pricehistorie; public List<Double> Pricehistorie { get { return _pricehistorie; } set { _pricehistorie = value; } } public Pricemethod Pricemethod { get; set; } public double Price { get { switch (Pricemethod) { case Pricemethod.Average: return Average(); case Pricemethod.Max: return Max(); case

what's the difference between the patterns Strategy, Visitor and Template Method?

萝らか妹 提交于 2019-12-03 03:19:54
问题 I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (: 回答1: Both the visitor, the strategy, and the template pattern encompass the application of an algorithm. The biggest difference is in how they are evoked and how they are used in practice. While it may seem like they have the same use case, look at

Difference between Strategy pattern and Command pattern

十年热恋 提交于 2019-12-03 02:03:14
问题 What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java. 回答1: Typically the Command pattern is used to make an object out of what needs to be done -- to take an operation and its arguments and wrap them up in an object to be logged, held for undo, sent to a remote site, etc. There will tend to be a large number of distinct Command objects that pass through a given point in a system over time, and the Command objects will hold