I'm developing application using Command Pattern. I have two kind of operations in my programm: 1. Long time operations (copy/delete files), which should be run asynchronously 2. Simple operations (refresh panels)
My question is: Can I implement 2 kind of commands in this Pattern (AsyncCmd and SimpleCmd)? And before Executing in Command Manager check type of command:
public void Execute()
{
if(cmd is AsyncCmd)
{
RunAsyncOperation();
}
else if(cmd is SimpleCmd)
{
RunOperation();
}
}
You could , but using if else for defining the type of operation defeats the purpose of the command patter . The asynchronous and simple (I guess synchronous) should be separate commands each inheriting from ICommand (which has one method Execute()) . These commands should be created using factories which can deal with command specific contexts , e.g. Anything special you have to do for asynchronous operations etc.