战略格局与指挥模式的差异 [英] Difference between Strategy pattern and Command pattern

查看:90
本文介绍了战略格局与指挥模式的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

策略模式命令模式?我也在寻找一些Java的例子。

What is the difference between the Strategy pattern and the Command pattern? I am also looking for some examples in Java.

推荐答案

通常,命令模式用于使一个对象从需要完成 - 将操作及其参数进行包装,并将其包含在要记录的对象中,用于撤销,发送到远程站点等。通常会有大量不同的Command对象通过给定点一个系统随着时间的推移,Command对象将保存描述所请求操作的不同参数。

Typically the Command pattern is used to make an object out of what needs to be done -- to take an operation and its arguments and wrap them up in an object to be logged, held for undo, sent to a remote site, etc. There will tend to be a large number of distinct Command objects that pass through a given point in a system over time, and the Command objects will hold varying parameters describing the operation requested.

另一方面,策略模式用于指定应该做一些事情,并插入更大的对象或方法来提供一个特定的算法。排序策略可能是合并排序,可能是插入排序,或者可能是更复杂的东西,如果列表大于最小大小,则仅使用合并排序。策略对象很少受到关于Command对象的批量混洗,而是经常用于配置或调优。

The Strategy pattern, on the other hand, is used to specify how something should be done, and plugs into a larger object or method to provide a specific algorithm. A Strategy for sorting might be a merge sort, might be an insertion sort, or perhaps something more complex like only using merge sort if the list is larger than some minimum size. Strategy objects are rarely subjected to the sort of mass shuffling about that Command objects are, instead often being used for configuration or tuning purposes.

这两种模式都涉及到代码分解将原始类中的各个操作的参数包含在另一个对象中以提供独立的可变性。差异在于实践中遇到的用例和每个模式背后的意图。

Both patterns involve factoring the code and possibly parameters for individual operations out of the original class that contained them into another object to provide for independent variability. The differences are in the use cases encountered in practice and the intent behind each pattern.

这篇关于战略格局与指挥模式的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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