使用策略模式和命令模式 [英] Using a strategy pattern and a command pattern

查看:90
本文介绍了使用策略模式和命令模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两种设计模式都封装了一个算法,并将实现细节与其调用类分离。唯一的区别是我们可以看出,策略模式需要参数进行执行,而Command模式则没有。

Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern takes in parameters for execution, while the Command pattern doesn't.

在我看来,命令模式需要所有用于执行的信息在创建时可用,并且能够延迟其调用(可能作为脚本的一部分)。

It seems to me that the command pattern requires all information for execution to be available when it is created, and it is able to delay its calling (perhaps as part of a script).

什么决定指导是使用一种模式还是另一种模式?

What determinations guide whether to use one pattern or the other?

推荐答案

我将包含几个GoF设计模式的封装层次表,以帮助解释这两种模式之间的区别。希望它更好地说明了每个封装的内容,所以我的解释更有意义。

I'm including an encapsulation hierarchy table of several of the GoF design patterns to help explain the differences between these two patterns. Hopefully it better illustrates what each encapsulates so my explanation makes more sense.

首先,层次结构列出了给定模式适用的范围,或适当的模式用于封装某些级别的细节,具体取决于您从哪一侧开始。

First off, the hierarchy lists the scope for which a given pattern is applicable, or the appropriate pattern to use to encapsulate some level of detail, depending on which side of the table you start at.

从表中可以看出,Strategy Pattern对象隐藏了算法实现的细节,所以使用不同的策略对象将执行相同的功能,但是以不同的方式。每个策略对象可能针对特定因素进行优化,或者对其他参数进行操作;并且通过使用通用接口,上下文可以安全地使用。

As you can see from the table, a Strategy Pattern object hides details of an algorithm's implementation, so the use of a different strategy object will perform the same functionality but in a different way. Each strategy object might be optimized for a particular factor or operate on some other parameter; and, through the use of a common interface, the context can safely work with either.

命令模式封装了比算法更小的细节级别。它将发送消息所需的细节编码到对象:receiver,selector和arguments。客观化过程执行的这样一小部分的好处是可以通过一般方式在不同的时间点或位置上调用这些消息,而无需对其细节进行硬编码。它允许消息被调用一次或多次,或者被传递到系统或多个系统的不同部分,而不需要在执行之前知道特定调用的细节。

The Command Pattern encapsulates a much smaller level of detail than an algorithm. It encodes the details needed to send a message to an object: receiver, selector and arguments. The benefit to objectifying such a tiny part of the process execution is that such messages can be invoked along different points of time or location in a general way without having to hard-code its details. It allows messages to be invoked one or more times, or passed along to different parts of the system or multiple systems without requiring the details of a specific invocation to be known before execution.

像设计模式的典型代码一样,它们并不要求所有的实现都具有相同的细节来承载模式名称。详细信息可能因实现而异,数据在对象中编码,而不是方法参数。

As is typical for design patterns, they do not require all implementations to be identical in detail to bear the pattern name. Details can vary in implementation and in what data is encoded in the object versus as method arguments.

这篇关于使用策略模式和命令模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆