strategy-pattern

When and How Strategy pattern can be applied instead of decorator pattern?

房东的猫 提交于 2019-11-29 06:46:30
I am learning design patterns and trying to follow Go4 book. On page:179, in the decorator pattern chapter, there is a line which says "..by extending the number of strategies from just one to an open-ended list, we achieve the same effect as nesting decorators recursively." I didn't quite get this statement. Strategies focus on having independent algorithms, which can be set dynamically and don't know much about the client they are set in. Whereas decorators are not quite independent of the clients they decorate. In fact, they are of same supertype as the object they decorate. Am I missing a

How to use fields in java enum by overriding the method? [duplicate]

你说的曾经没有我的故事 提交于 2019-11-28 22:39:11
This question already has an answer here: Why can enum implementations not access private fields in the enum class 4 answers The task is to implement beautiful strategy design pattern with the java enum : public enum MyEnum { FIRST { @Override public String doIt() { return "1: " + someField; //error } }, SECOND { @Override public String doIt() { return "2: " + someField; //error } }; private String someField; public abstract String doIt(); } but when referring to someField I get Cannot make a static reference to the non-static field someField. What is wrong and is it possible to do that better

Strategy Pattern with no 'switch' statements?

柔情痞子 提交于 2019-11-28 15:28:28
I've been doing some reading on the Strategy Pattern, and have a question. I have implemented a very basic Console Application below to explain what I'm asking. I have read that having 'switch' statements is a red flag when implementing the strategy pattern. However, I can't seem to get away from having a switch statement in this example. Am I missing something? I was able to remove the logic from the Pencil , but my Main has a switch statement in it now. I understand that I could easily create a new TriangleDrawer class, and wouldn't have to open the Pencil class, which is good. However, I

What is the difference between the template method and the strategy patterns?

╄→гoц情女王★ 提交于 2019-11-28 13:10:45
问题 Can someone please explain to me what is the difference between the template method pattern and the strategy pattern is? As far as I can tell they are 99% the same - the only difference being that the template method pattern has an abstract class as the base class whereas the strategy class uses an interface that is implemented by each concrete strategy class. However, as far as the client is concerned they are consumed in exactly the same way - is this correct? 回答1: The main difference

Strategy Pattern and Dependency Injection using Unity

老子叫甜甜 提交于 2019-11-27 19:04:51
I am finally getting my feet wet with Dependency Injection (long overdue); I got started playing with Unity and run into an issue with the strategy pattern. I can use the container to return to me specific implementations of a strategy based on a name, but what I don't see is how I am supposed to get the right strategy in the context. Let's illustrate on a simple example: the context is a car, which has an IEngine (the strategy), with 2 implementations, FastEngine and SlowEngine. The code would look along these lines: public interface IEngine { double MaxSpeed { get; } } internal class

What is the difference between the bridge pattern and the strategy pattern?

安稳与你 提交于 2019-11-27 17:21:55
I tried to read many articles on dofactory , wikipedia and many sites. I have no idea on differences between bridge pattern and the strategy pattern. I know both of them decouple an abstraction from its implementation and can change implementation at run time. But I still don't know in which situation I should use strategy or in which situation I should use bridge. Kent Boogaart Semantics. From wikipedia : The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy

What is the difference between Strategy pattern and Dependency Injection?

本小妞迷上赌 提交于 2019-11-27 16:49:17
Strategy pattern and Dependency Injection both allow us to set / inject objects at run time. What is the difference between Strategy pattern and Dependency Injection? DI and Strategy work in the same way, but Strategy is used for more fine-grained and short-lived dependencies. When an object is configured with a "fixed" Strategy, for example when the object is constructed, the distinction between Strategy and DI blurs. But in a DI scenario it is more unusual that the dependencies of objects change during their lifetimes, while this is not uncommon with Strategy. Also, you can pass strategies

Modifying if-else to strategy pattern

自古美人都是妖i 提交于 2019-11-27 15:16:00
问题 I have the following if-else branch in java. if (str.equals("a")) { A;} else if (str.equals("b")) { B;} else if (str.equals("c")) { C;} else if (str.length == 5) { D;} else { E;} how to modify this code into strategy pattern ? 回答1: Here an example of a strategy pattern using a factory: public interface Strategy { public Object[] execute(Object[] args); } public class StrategyFactory { public enum Name { REVERSE, STRINGIFY, DUPLICATE; } private StrategyFactory() { // never instantiate; only

How to use fields in java enum by overriding the method? [duplicate]

我与影子孤独终老i 提交于 2019-11-27 14:36:14
问题 This question already has answers here : Why can enum implementations not access private fields in the enum class (4 answers) Closed 4 years ago . The task is to implement beautiful strategy design pattern with the java enum : public enum MyEnum { FIRST { @Override public String doIt() { return "1: " + someField; //error } }, SECOND { @Override public String doIt() { return "2: " + someField; //error } }; private String someField; public abstract String doIt(); } but when referring to

What is the difference between Factory and Strategy patterns?

拜拜、爱过 提交于 2019-11-27 10:14:04
Can any one explain the difference between factory and strategy patterns? For me both are looking same other than an extra factory class (which create an object of product in factory patterns) A factory pattern is a creational pattern. A strategy pattern is an operational pattern. Put another way, a factory pattern is used to create objects of a specific type. A strategy pattern is use to perform an operation (or set of operations) in a particular manner. In the classic example, a factory might create different types of Animals: Dog, Cat, Tiger, while a strategy pattern would perform