Command Design Pattern - TechNet Articles - United States (English) - TechNet Wiki

This article describes the Command design pattern. This is a behavioral design pattern, a category of design pattern used by software engineers, when writing computer programs.

Table of Contents

Introduction

The Command pattern is a design pattern, used in software engineering, to encapsulate everything that is needed to execute a method at a later time. The three main concepts in this pattern are client, invoker and receiver. Firstly the client defines the command and it's properties. It is then called by invoker and sent to the receiver, which takes the properties and performs it's actions based on them.

It is defined as a behavioral design pattern because it affects program execution.


Benefits

The command pattern allows developers to construct a unified message structure to encapsulate a method into a delegate, which can be resused by different owners, with different parameters.

Examples of the pattern

Commands can be used to build a history of actions, to enable record a macro, enable an undo function, or list of "roll-back" transactions.

Messages can also inform progress bars or wizards.

Commands are also useful across threads and in parallel processing in general.

In WPF, commanding allows the developer to separate the semantics of the operation, from the logic that executes the command. Examples are Print, Copy and Paste. These commands are already baked into WPF and available to use, but the implementation of the command is down to the application (Copy text, or copy a class. Print to file, or print to printer). Commands implement the ICommand interface, which also defines CanExecute and Execute delegates, to determine if the command can be executed, and what method is called upon execution. When a command is tied to a button, the CanExecute method controls the 'enabled' state of the button.

Read more and download the sample from here

code.msdn.microsoft.com/Design-Patterns-Command-1962d567