Command Design Pattern
Today I'm going to show you another important design pattern which we can use for recurring problems. Main intent of this design pattern are mention below
1. ActionList Enum
2. IReciever Interface
3. Calculator Class
4. Command Class
5. Add Command Class
6. Multiply Command Class
7. Subtract Command Class
Okay now the the core part of the design pattern is over. it time to check how it works. to check this we need to create an form. so go to your project and add a window form or else you can use the default one and create a user interface as shown below
8. Code behind of Window form
Hmm so now you have a great example for the command pattern. so go put a breakpoint (F9) in middle of CalculateButtonClick event and press build button (F5). then you can see the magic of command pattern. happy coding guys. so sleepy here. Have a nice day. please put a comment if you have any question. suggestions are also welcome. see you later.
- Encapsulate a request as an object, thereby letting you parameterize clients with different request, queue or log request, and support undoable operations.
- Promote "Invocation of a method on an object" to full object status.
- Promote An object-oriented callback
1. ActionList Enum
/// <summary>
/// Action List Enum
/// </summary>
public enum ActionList
{
Add,
Substract,
Multiply
}
2. IReciever Interface
/// <summary>
/// IReciever Interface
/// </summary>
public interface IReciever
{
/// <summary>
/// Sets the action.
/// </summary>
/// <param name="action">The action.</param>
void SetAction(ActionList action);
/// <summary>
/// Gets the result.
/// </summary>
/// <returns></returns>
int RetrieveResult();
}
3. Calculator Class
/// <summary>
/// Calculator Class
/// </summary>
public class Calculator : IReciever
{
#region Variable Declaration
/// <summary>
/// The value one
/// </summary>
private readonly int valueOne;
/// <summary>
/// The value two
/// </summary>
private readonly int valueTwo;
/// <summary>
/// The action list
/// </summary>
private ActionList actionList;
#endregion
#region Constructor Declaration
/// <summary>
/// Initializes a new instance of the <see cref="Calculator"/> class.
/// </summary>
/// <param name="valueX">The value X.</param>
/// <param name="valueyY">The value Y.</param>
public Calculator(int valueX, int valueyY)
{
this.valueOne = valueX;
this.valueTwo = valueyY;
}
#endregion
#region Public Method Declaration
/// <summary>
/// Sets the action.
/// </summary>
/// <param name="action">The action.</param>
public void SetAction(ActionList action)
{
this.actionList = action;
}
/// <summary>
/// Gets the result.
/// </summary>
/// <returns></returns>
public int RetrieveResult()
{
int result = 0;
switch (this.actionList)
{
case ActionList.Add:
result = this.valueOne + this.valueTwo;
break;
case ActionList.Substract:
result = this.valueOne - this.valueTwo;
break;
case ActionList.Multiply:
result = this.valueOne * this.valueTwo;
break;
}
return result;
}
#endregion
}
4. Command Class
/// <summary>
/// Command Class
/// </summary>
public abstract class Command
{
internal IReciever Reciever = null;
/// <summary>
/// Initializes a new instance of the <see cref="Command"/> class.
/// </summary>
/// <param name="reciever">The reciever.</param>
protected Command(IReciever reciever)
{
this.Reciever = reciever;
}
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public abstract int Execute();
}
5. Add Command Class
/// <summary>
/// Concrete Command Class
/// </summary>
public class AddCommand : Command
{
/// <summary>
/// Initializes a new instance of the <see cref="AddCommand"/> class.
/// </summary>
/// <param name="reciever">The reciever.</param>
public AddCommand(IReciever reciever): base(reciever)
{
}
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public override int Execute()
{
this.Reciever.SetAction(ActionList.Add);
return this.Reciever.RetrieveResult();
}
}
6. Multiply Command Class
/// <summary>
/// Multiply Command Class
/// </summary>
public class MultiplyCommand :Command
{
public MultiplyCommand(IReciever reciever): base(reciever)
{
}
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public override int Execute()
{
this.Reciever.SetAction(ActionList.Multiply);
return this.Reciever.RetrieveResult();
}
}
7. Subtract Command Class
/// <summary>
/// Subtract Command Class
/// </summary>
public class SubtractCommand : Command
{
public SubtractCommand(IReciever reciever): base(reciever)
{
}
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns></returns>
public override int Execute()
{
this.Reciever.SetAction(ActionList.Substract);
return this.Reciever.RetrieveResult();
}
}
Okay now the the core part of the design pattern is over. it time to check how it works. to check this we need to create an form. so go to your project and add a window form or else you can use the default one and create a user interface as shown below
8. Code behind of Window form
/// <summary>
/// Form1 Class
/// </summary>
public partial class Form1 : Form
{
#region Variable Declaration
/// <summary>
/// The Base command
/// </summary>
private Command command;
/// <summary>
/// The add command
/// </summary>
private AddCommand addCommand;
/// <summary>
/// The multiply command
/// </summary>
private MultiplyCommand multiplyCommand;
/// <summary>
/// The subtract command
/// </summary>
private SubtractCommand subtractCommand;
#endregion
#region Constructor Declaration
/// <summary>
/// Initializes a new instance of the <see cref="Form1"/> class.
/// </summary>
public Form1()
{
InitializeComponent();
}
#endregion
#region Form Events Declaration
/// <summary>
/// Initializes the commander.
/// </summary>
/// <param name="valueOne">The value one.</param>
/// <param name="valueTwo">The value two.</param>
private void InitializeCommander(int valueOne, int valueTwo)
{
var reciever = new Calculator(valueOne, valueTwo);
this.addCommand = new AddCommand(reciever);
this.subtractCommand = new SubtractCommand(reciever);
this.multiplyCommand = new MultiplyCommand(reciever);
}
/// <summary>
/// Checks which radio button is checked
/// </summary>
private void CheckCommand()
{
if (this.addRadioButton.Checked)
{
this.command = this.addCommand;
}
else if (this.SubstractRadioButton.Checked)
{
this.command = this.subtractCommand;
}
else if (this.MultiplyRadioButton.Checked)
{
this.command = this.multiplyCommand;
}
}
/// <summary>
/// Handles the Click event of the CalculateButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void CalculateButtonClick(object sender, EventArgs e)
{
var valueOne = Convert.ToInt32(this.ValueOneText.Text,CultureInfo.InvariantCulture);
var valueTwo = Convert.ToInt32(this.ValueTwoText.Text, CultureInfo.InvariantCulture);
this.InitializeCommander(valueOne,valueTwo);
CheckCommand();
ResultLable.Text = command.Execute().ToString(CultureInfo.InvariantCulture);
}
#endregion
}
Hmm so now you have a great example for the command pattern. so go put a breakpoint (F9) in middle of CalculateButtonClick event and press build button (F5). then you can see the magic of command pattern. happy coding guys. so sleepy here. Have a nice day. please put a comment if you have any question. suggestions are also welcome. see you later.
Comments
Post a Comment