---
title: "19-Command: choosing the operation at run-time"
author: "ccfangle"
date: "2012-02-01T17:45:04Z"
tags: []
categories: ["编程语言"]
slug: "19-command-choosing-the-operation-at-run-time"
draft: false
lastmod: "2012-02-01T20:48:55Z"
id: 1389160
url: "https://ccfangle.iteye.com/blog/1389160"
---

A Command is a function object in its purest sense: a method that’s an object[](#_ftn7) . By wrapping a method in an object, you can pass it to other methods or objects as a parameter, to tell them to perform this particular operation in the process of fulfilling your request. **You could say that a *Command* is a messenger (because its intent and use is very straightforward) that carries behavior, rather than data.**

    ......

     The primary point of *Command* is to allow you to hand a desired action to a method or object. **In the above example, this provides a way to queue a set of actions to be performed collectively. In this case, it allows you to dynamically create new behavior, something you can normally only do by writing new code** but in the above example could be done by interpreting a script (see the *Interpreter* pattern if what you need to do gets very complex).

     以上描述是对Command设计模式的很好阐述，正如题目中Command设计模式就是在运行中选择“operation”，只要将“opration”按照自己的需求，实现接口就可以了，达到了调用方和“operation”解耦的目的。

     具体的应用学习，在[http://lgd-java2eye.iteye.com/blog/757524](http://lgd-java2eye.iteye.com/blog/757524) 中有很好的解释，这里不赘述。