strategy-pattern

Using the Strategy Pattern to avoid downcasting

主宰稳场 提交于 2019-12-13 15:29:30
问题 I was reading on this site about the Liskov substitution principle. It states: As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes must be substitutable for the base class. According to this page, if you override a method in the base class and it does nothing or throws an exception, you're in violation of the principle. Suppose I had an abstract class called Weapon , and the

How To Have A Single Interface Return Different Data Types?

血红的双手。 提交于 2019-12-13 14:24:03
问题 In a nutshell, I want to use a single interface, IProducer , to create an object, IProduct . IProduct will have different components depending on which interface created it. The IProduct class will then be used by the IConsumer interface. The correct IConsumer class should be used (I do not want to do type checking myself) based on the derived type of IProduct . I would essentially like to use the Strategy pattern (different behaviors behind a single interface), but with the added ability to

Confused about strategy design pattern

牧云@^-^@ 提交于 2019-12-13 13:10:06
问题 I can not understand why to use Context module(which we will see in the following codes) in strategy design pattern, what its function? Let's see one part of the strategy design pattern. public interface Strategy { public int doOperation(int num1, int num2); } public class OperationAdd implements Strategy { @Override public int doOperation(int num1, int num2) { return num1 + num2; } } public class OperationSubstract implements Strategy { @Override public int doOperation(int num1, int num2) {

Where is the benefit in using the Strategy Pattern?

ぐ巨炮叔叔 提交于 2019-12-13 11:33:12
问题 I've looked at this explanation on Wikipedia, specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example? What I can make from the answers so far, it seems

Multiple Buttons, 1 ClickListener using generics, Strategy Pattern - All buttons only do the same operation

淺唱寂寞╮ 提交于 2019-12-13 04:19:10
问题 I have been looking at different ways of making Listeners in Android. I encapsulated the listener in a separate class called Click . I have also used the strategy pattern to encapsulate logic and that is interfaced with the enum type BinaryOperator . What I have is a simple calculator that you probably seen online already - 2 input numbers, 1 result, and 4 buttons (+, -, *, and /). There is an error in my code, because no matter what button I press, it only does the division operation. I

Is using OnClickListener() an example of Strategy pattern?

本小妞迷上赌 提交于 2019-12-12 18:21:05
问题 Is OnClickListener in android an example of Strategy pattern ? In another Stackoverflow question accepted answer says it is Observer Pattern. Similar Code to understand the question. public interface OnClickListener{ void onClick(View view); } public class Button extends View{ private OnClickListener listener; void clicked(){ //some code if(listener != null){ listener.onClick(this); } //some other code } public void setOnClickListener(OnClickListener listener){ this.listener = listener; } }

Parameterize a generic interface - Create a Dictionary<Type, Interface<T>>?

走远了吗. 提交于 2019-12-12 13:54:15
问题 Sorry the title is confusing, I don't know if I said it right, not sure what this thing is called... Feel free to edit it after you read the question if you want. I'm refactoring my old code when I noticed there are a lot of places where I could use the Strategy pattern. I have an inventory system, and items - There are more than one ways to add an item - Normal way, forceful way, etc. You could also swap items, again, with more than one way - So I figured those are good places to use that

Strategy Pattern with Dummy concrete strategy

南笙酒味 提交于 2019-12-11 11:55:15
问题 Refering to posted question here, could you please comment if this is good approach to solve OPTIONAL behaviour for extending class, by using composition, not inheritance. Plannable behaviour is here extended by Strategy pattern. So, class Task could optionally have any combination of various behaviours. Plannable is just one of them, therefore inheritance does obviously not making sense here. Question is what to do when Task does not have particular behaviour ? I see few possible approaches:

Refactor with strategy pattern and then apply SOLID principle

落花浮王杯 提交于 2019-12-11 02:55:54
问题 I have a C# class like below in an app, and looking at ways to refactor it. The Send method does not exist in the class. This is the solution that I came up with. There will be more email types in the future. I don't know whether I can apply the SOLID Open/Closed principle here because adding a new emailtype require this class to be modified. The Consumer of this service should not be concerned about the business logic, but just to know only the new emailType and the customerId . The consumer

Mobile strategy: How to handle this server-client version permutation?

百般思念 提交于 2019-12-10 17:12:31
问题 We are hosting a SAAS application for clients. The main app is a web appliction and different clients can be on different versions. e.g.: Company A version 1.0 Company B version 1.1 We also have Android/iOS apps in App Store. Since the apps are not always up to date, we can have multiple app versions in the market too. e.g. 2.0 and 2.1 . Thus for compatibility reason, we have to make sure that each client version works with each server version: server 1.0 must work with client 2.0, server 1.0