简体   繁体   English

执行两个或多个算法的策略模式

[英]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? 也许我要在列表中插入这些算法,然后使用a执行该列表中的所有算法?

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! 您只有1个策略!

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. 在run方法中,调用管道,该管道又依次调用每个算法。 As New events are raised by system to say a new processor is available, add it to the pipeline. 当系统引发新事件以说有新处理器可用时,请将其添加到管道中。

edit2 编辑2

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. 如果具有run方法的对象是模式中的主题,并且添加新处理器时,可以调用addsubscriber方法将处理器添加到列表中,则可以使用观察者模式(虽然不是很合适的恕我直言)来实现它。 Then when run is called you iterate the list of subscribers and pass them the sound to modify. 然后,在调用run时,您将迭代订阅者列表,并将修改后的声音传递给他们。

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. 通常,这是通过策略完成的:处理程序都是策略的所有实现(在Java中,这是一个接口),然后您可以将其保存在地图中,然后根据类似地图类型的内容来分配适当的地图传入请求,也可以将它们放在列表中并遍历它们,找到合适的请求。

If you let everyone just get a crack at handling the message, you have a Chain of Responsibility. 如果您让每个人都只是在处理信息上有所分歧,那么您就有责任链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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