Strategy pattern和Command pattern之间有什么区别?我也在寻找Java中的一些例子。
答案 0 :(得分:99)
通常,Command模式用于从需要完成的操作中创建一个对象 - 获取操作及其参数并将它们包装在要记录的对象中,保留用于撤消随着时间的推移,将会有大量不同的Command对象通过系统中的给定点,并且Command对象将包含描述所请求操作的不同参数。
另一方面,策略模式用于指定如何应该完成的事情,并插入更大的对象或方法以提供特定的算法。排序策略可能是合并排序,可能是插入排序,也可能是更复杂的事情,如果列表大于某个最小大小,则仅使用合并排序。策略对象很少受到Command对象的大规模混乱,而是经常用于配置或调优目的。这两种模式都涉及将包含它们的原始类中的单个操作的代码和可能的参数分解为另一个对象以提供独立的可变性。不同之处在于实践中遇到的用例以及每种模式背后的意图。
答案 1 :(得分:42)
已经给出了单词。这是具体代码的不同之处。
public class ConcreteStrategy implements BaseStrategy {
@Override
public void execute(Object argument) {
// Work with passed-in argument.
}
}
public class ConcreteCommand implements BaseCommand {
private Object argument;
public ConcreteCommand(Object argument) {
this.argument = argument;
}
@Override
public void execute() {
// Work with own state.
}
}
答案 2 :(得分:35)
策略 - Quicksort或Mergesort [algo change]
命令 - 打开或关闭[操作更改]
答案 3 :(得分:3)
主要区别在于,命令对对象执行一些操作。 它可能会改变对象的状态。
战略决定如何处理对象。 它封装了一些业务逻辑。
答案 4 :(得分:3)
策略模式非常有用。
HttpServlet代码中的一个很好的例子:
service()
方法会根据方法类型将用户的请求指向doGet()或doPost()或其他方法。
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
战略模式的突出特点
命令模式用于启用Invoker和Receiver之间的松散耦合。 Command,ConcreteCommand,Receiver,Invoker和Client是这种模式的主要组成部分。
不同的接收者将通过Invoker&amp;执行相同的命令。具体命令但Command的实现会因每个Receiver而异。
e.g。你必须实施&#34; On&#34;和&#34;关&#34; TV&amp; amp;的功能DVD播放机。但是TV和DVDPlayer对这些命令会有不同的实现。
使用代码示例查看以下帖子:
答案 5 :(得分:0)
我认为这里的最大区别是,当您需要在实现 相同接口 的不同对象之间进行混洗时,会使用策略模式,但是命令模式用于在实现 不同接口 的某些对象之间进行随机播放(因为它们将它们封装到称为“命令对象”的其他对象中),并且像策略模式一样传递这些命令对象。