With the hope of learning the Command pattern, I have set up a simple GUI application in C# with two buttons. The first button uses (my attempt at) the Command pattern to perform an operation, whereas the second simply calls a static method in another class.
Here is the form code
private void CommandButton_Click(object sender, EventArgs e)
{
ICommand command = new ConcreteCommand();
command.Execute(); //Performs the same code as DoOperation()
}
private void StaticButton_Click(object sender, EventArgs e)
{
Helper.DoOperation(); //Performs the same code as Execute()
}
My questions are:
- Is it appropriate to to implement the Command Pattern in this scenario?
- What advantages/disadvantages are associated with using the Command Pattern over static method calls in this specific case? In other, "real world" scenarios?
- If my GUI design changes in the future, would it be any more difficult or would it imply more changes to existing code if I used static function calls instead of the Command Pattern?
Command Pattern is used to do set of operations based on the value in command argument, whereas in the code you have shown there is no dependency on the current state. In your case I static method would be fine.
But this wont always be the case so study well your need and then opt for the way to proceed, I would have gone for Command Pattern as this gives more flexibility with future change to requirement.