Command 模式将一个请求封装为一个对象——将一个操作转化为一个独立的对象,包含所有相关信息。这使得排队、日志记录、撤销/重做和将请求者与执行者解耦成为可能。
Command 模式的功能
COMMAND → encapsulate an action/request as an OBJECT (with its data and an execute method):
→ a command object knows WHAT to do and has the info to do it
→ the invoker triggers commands without knowing their details
→ decouples the object that INVOKES from the object that PERFORMS the action
→ "turn an action into an object" → store, pass, queue, log, undo it
示例
{
() { . = doc; . = text; }
() { ..(.); }
() { ..(.); }
}
cmd = (doc, );
cmd.();
cmd.();
