简体   繁体   中英

Strategy pattern executing two or more algorithms

Can anyone make me an example of a strategy pattern that use not one, but two or more algorithms in sequence??

Maybe have I to insert those algorithms in a list and then with a for execute all algorithms in this list?

This list must be a public attribute of context class?

Please, can anyone make me a pseudo-code example?

You could implement a strategy, which invokes all algorithms in specific order. My example is linked with classes described in Strategy pattern :

class MultiAlgorithm implements Strategy {

    private Strategy[] strategies;

    public MultiAlgorithm(Strategy... strategies) {
        if (strategies == null || strategies.length == 0) {
            throw new IllegalArgumentException(
                    "Algorithms collection cann't be null!");
        }
        this.strategies = strategies;
    }

    @Override
    public int execute(int a, int b) {
        System.out.println("Called MultiAlgorithm's execute()");
        int result = 0;
        for (Strategy strategy : strategies) {
            result += strategy.execute(a, b);
        }
        return result;
    }
}

Example of usage

public static void main(String[] args) throws Exception {
    Context context = new Context(new MultiAlgorithm(new Add(),
            new Multiply(), new Subtract()));
    int result = context.executeStrategy(1, 2);
    System.out.println(result);
}

As you see, we must only implement new "complicated strategy". Pattern himself stayed without changes.

You could just have an implementation of your strategy that was a composite of two instances of your strategy which were executed in sequence couldn't you?

Although I can't think of a situation in which this would be useful.

Exactly what you want to do is not clear.

** based on your comments below **

I don't think the strategy patten is appropriate here as you are always doing a fixed thing, applying all the implementations of noise filters that you have, and the strategy pattern is for choosing between different approaches. You only have 1 strategy!

I would implement this as a processing pipeline. Create the pipeline with all algorithms you know about at the start in the order you want to apply them. In the run method invoke the pipeline which in turn invokes each of the algorithms in turn. As New events are raised by system to say a new processor is available, add it to the pipeline.

edit2

You could probably implement it using the observer pattern (though not a great fit imho) if the object with the run method is the subject in the pattern and when new processors are added the addsubscriber method is called to add the processor to the list. Then when run is called you iterate the list of subscribers and pass them the sound to modify.

But that feels all sorts of wrong to me.

Sounds like what your assignment is trying to get at is that you have some outer code (perhaps a controller) that has to decide at runtime which approach to use to handle something. Quite often this is done with Strategy: the handlers are all implementations of the Strategy (in Java, this is an interface), and then you could either hold them in a map where you dispatch the appropriate one based on something like the type of the incoming request, or you could hold them in a list and loop through them and find the one that's suitable.

If you let everyone just get a crack at handling the message, you have a Chain of Responsibility.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM