Parameterized Strategy Template

I have several Java classes that implement a strategy template. Each class has variable parameters for the number of different types:

interface Strategy { public data execute(data); } class StrategyA implements Strategy { public data execute(data); } class StrategyB implements Strategy { public StrategyB(int paramA, int paramB); public data execute(data); } class StrategyC implements Strategy { public StrategyC(int paramA, String paramB, double paramC); public data execute(data); } 

Now I want the user to be able to enter parameters into some kind of user interface. The user interface should be selected at runtime, i.e. Strategies should be independent of it. The parameter dialog should not be monolithic, and it should be possible to make it behave and look different for each strategy and user interface (for example, the console or Swing).

How would you solve this problem?

+7
java oop design-patterns model-view-controller strategy-pattern
source share
4 answers

One opportunity to do this with something similar to the Builder design pattern:

For each type of strategy, you must have an appropriate builder (one or more). The builder does not work as a regular constructor, receiving all init parameters as arguments to a method; Instead, it should block until an appropriate input is received. Some developers will display the Swing dialog and wait, others will print to the console and wait for input, others may read from the file, etc. After the builder receives all the inputs, he can create an instance of the strategy and return it.

Thus, you separate the logic of data search from the strategy itself. Another advantage is that you can have a common interface for all developers, therefore, as soon as you select a specific builder, you can manipulate it with the same piece of code.

+4
source share

The solution to this problem depends mainly on what determines which strategy is current. For simplicity, I assume that the user interface is the same for all strategies.

In practice, you will create a builder class or factory method. Something along this line:

 interface StrategyPicker { public Strategy getStrategy(); } // Most likely used in the JFrame it is added to class StrategyPickerUI extends JPanel implements StrategyPicker { // initialize the panel with all the widgets // and implement the getStrategy method. the getStrategy // method should be called after the input is done in this // panel (such as clicking an Ok or Apply button) } // You can also make one for the console app class StrategyPickerSimple implements StrategyPicker { // ... } 

If you want to be real, you will create a simple factory class to remove the act of creating your own class in it:

 public class StrategyFactory() { public static Strategy createStrategyFromParameters(StrategyParams sp) { // creates the Strategy object... doesn't need to be public static // and if it isn't, it will help making unit tests easier } // This nested class could be split up to StrategyAParams, // StrategyBParams, StrategyCParams public class StrategyParams { data paramA; int paramB_A; int paramB_B; int paramC_A; String paramC_B; float paramC_C; } } // in StrategyPickerUI class public getStrategy() { StrategyParams sp = new StrategyParams(); sp.paramB_A = myJTextFieldParamB_A.getText(); // and so on... return StrategyFactory.createStrategyFromParameters(sp); } 

If you want the user interface not to be monolithic, divide the responsibilities into your own objects. Hope this helps.

+1
source share

If the parameter classes contain a simple object (Numbers, Boolean, Date, String), you can try to create your interface at runtime.

Itโ€™s more difficult to create a user interface for composite objects and a set of parameters

Check out Metawidget is a powerful ui generator.

0
source share

First I would suggest googling "Parameterized Strategy Pattern". There is an article that may be exactly what you are looking for http://www.hillside.net/plop/2010/papers/sobajic.pdf

0
source share

All Articles