problem about strategy-pattern-Collection of common programming errors
SnOrfus
c# design-patterns strategy-pattern
I’ve been going through Head First Design Patterns (just came in recently) and I was reading about the strategy pattern, and it occurred to me that it might be a great way to implement a common way of calculating taxes etc. on all of the particular objects I use at work, but I had a question about it.Here’s what I was thinking:public interface ITax {decimal ProvincialTaxRate { get; set; } // Yes, I’m Canadian :)decimal CalculateTax(decimal subtotal); }public SaskatchewanTax {public decimal Provi
MattK
design-patterns scala strategy-pattern
When I’m programming in Java (or a similar language), I often employ a simple version of the Strategy pattern, using interfaces and implementation classes, to provide runtime-selectable implementations of a particular concept in my code.As a very contrived example, I might want to have the general concept of an Animal that can make a noise in my Java code, and want to be able to select the type of animal at runtime. So I would write code along these lines:interface Animal {void makeNoise(); }cla
Lazer
design-patterns strategy-pattern state-pattern
What is the difference between Strategy Design pattern and State Design pattern? I was going through quite a few articles on the web but could not make out the difference clearly. Can somebody please explain in layman’s terms?
jorgen.ringen
design-patterns dependency-injection strategy-pattern
I’m kind of a beginner when it comes to design patterns. Any thoughts on implementing the strategy pattern/ like this:public class SomeClass {private Strategy strategy = new DefaultStrategy();public void provideCustomStrategy(Strategy strategy) {this.strategy = strategy;} }This will ensure loose coupling and all the other benefits of the Strategy Pattern and DI. At the same time you don’t force the user to provide a strategy, and the user can decide to provide a custom strategy for corner cases,
Krishna
strategy-pattern state-pattern
I was reading through this link about the state pattern. Is it looks like strategy pattern? What is the exact difference between these two patterns?
dav_i
c# design-patterns strategy-pattern
I am currently trying to get my head around all of the different Design Patterns, I have been set the task of sorting an IQueryable based on different columns, this is how it is currently implemented: if (choice == 1) { return from Animals in ctx.Animals orderby Animals.AnimalID descending select Animals; } else if (choice == 2) { return from Animals in ctx.Animals orderby Animals.Name descending select Animals; } else if (choice == 3) { return from Animals in ctx.Animals orderby Animals.Age d
Mark
Nilesh
OneMoreVladimir
java dependency-injection guice strategy-pattern
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 {@Injectprotected WeaponBehaviour weapon;public GameCharacter() {}public void fight() {weapon.useWeapon();}public void setWeapon(WeaponBehaviour weapon) {this.weapon = weapon;} }
Mark
oop design-patterns strategy-pattern
i hope you can help me with my problem:I have a Class doing soap calls. But if the soap definition changes i’ll have to write a new class or inherit from it etc. So I came to the solution to write something like that:switch(version) {case “1.0”:saopV1.getData()case “2.0”:soapV2.getData() }Well pretty bad code, i know. Then I read about the Strategy pattern and i thought, wow that’s what i need to get rid of this bad switch-case thing:abstract SoapVersion {public SoapVersion GetSoapVersion(string
Lazer
design-patterns strategy-pattern
When implementing the Strategy Pattern, where does one put the code that determines which strategy to use? Some sample pseudo-code would help.
Lazer
c++ design-patterns strategy-pattern
In the past, I have seen the strategy pattern explained as a mechanism which allows the user of a function/class to provide their own functionality for that function/class.I had always been taught that the way to implement the pattern was by taking function pointers into your classes/functions and calling them internally, thus allowing the programmer to provide their own “strategy” which would be used internally by those functions and objects.Looking around more recently, I see that the strategy
DarthVader
c# design-patterns dependency-injection strategy-pattern
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, testability, wiri
Optimight
design-patterns scala strategy-pattern
To explain my question:Class : ToyTrait1: Speak like MaleTrait2: Speak like FemaleCan I change the behavior (traits) of Toy during runtime so sometimes the same object speaks like male and sometimes the same object speaks like female?I want to change the speaking behavior at runtime.
Mark
design-patterns dependency-injection strategy-pattern
When implementing the strategy pattern, how do you determine which class is responsible for:Selecting the specific concrete strategy implementation to pass to the Context class (assuming that the selection is based on some complex business logic and not a static flag) Instantiating the aforementioned concrete implementation and actually injecting it into the Context classIt feels like there ought to be some objective guidance out there that covers this. I’ve done some reading on various OOP pat
Mark
c# strategy-pattern
I need to process a list of records returned from a service. However the processing algorithm for a record changes completely based on a certain field on the record. To implement this , I have defined an IProcessor interface which has just one method :public interface IProcessor { ICollection<OutputEntity> Process(ICollection<InputEntity>> entities); }And I have two concrete implementations of IProcessor for the different types of processing. The issue is that I need to us
Eduardo Rocha
java swing jtabbedpane strategy-pattern
I have a JTabbedPane like the one in this picture:I have a class for each tab (HouseGUI, CSPGUI, VPPGUI, and many others). Each class has a method called writeToXML()I need to call the writeToXML() method of each “class” in my JTabbedPane when I press the “Save All” button. But I don’t really know how to do it. Can you help me?Here’s what I’ve done so far:if (e.getSource() == saveAllButton) {int totalTabs = tabbedPane.getTabCount();ArrayList<ArrayList<String>> salvationForAll = new A
AlexMorley-Finch
php oop validation patterns strategy-pattern
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” => “Invalid Email Address” );But I’m really struggling on HOW the hell I’m going to do this!I’ve read countless pages on PHP validators and read that the best way to do this is with the strategy pattern
bmalets
ruby-on-rails-3 inheritance module config strategy-pattern
I’m writing project, which use APIs from other services. Now I’m trying to implement strategy pattern inside of project:I have BaseServise parent class ( base_services.rb ). PhoneNumbersService and ActivateService classes inherts BaseService class. In folder ‘services_api’ exists modules with api_methods.Code from files:# app/services/base_service.rbclass BaseServiseend# app/services/phone_numbers_service.rbclass PhoneNumbersService < BaseServiceinclude PhoneNumbersServiceApi def make
juanchopanza
c++ templates strategy-pattern
This is a sample implementation of Strategy Pattern in C++:ConcreteStrategy.hclass 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.htemplate <class Strategy> class MyCon
PHeiberg
php design-patterns undefined strategy-pattern
I’m new to PHP but come from a Java background and I’m trying to implement a simple strategy pattern in OO-PHP.I’m having a problem with variable scope and assigning an object to a class property. I get an error saying that the property $strategy is undefined when trying to access from the metric constructor. Can anyone help?Thanks, JohnStrategy Pattern Code:interface iMetric{public function calculateReadability($text);}/*Context – strategy pattern.*/ class metric{private $strategy;function __co
Web site is in building