Command Pattern In .NET
In Command Pattern, an object is used to represent and encapsulate all the information needed to call the method at a later time – method name, object that owns the method and values for the parameters. In the first part of an article series “The Command Pattern In .NET”, Eric Vogel walks through using the ICommand interface in the context of WPF applications.
A Command is normally an operation and a command object knows how to perform that operation. The command objects themselves implement a common interface – the invoker only needs to invoke the method by calling that interface, without knowing the implementation details. This helps in decoupling the object that invokes the operation from the one that knows how to perform it leading to clear separation of concerns.
The main purpose of using ICommand in Silverlight or WPF applications is to keep the XAML view-behind clean of any user-interaction code. The article “Using ICommand In Silverlight” is also a good read for understanding how to implement this in a Silverlight application. Since command objects can be queued for later invokation of their methods, this pattern is also quite useful in implementing Undo-Redo features.
Note that ICommand itself is not necessary for implementing Command Pattern – you could create your own interface or an abstract class for defining what a Command is. These samples show the command pattern in action without relying on the ICommand interface but using the same underlying concept.
Craig Motlin Sep 01, 2014