When I was looking this question, a question comes my mind.
Think about using an interface like :
public interface ICommandProcessor<T> where T : ICommand
{
void Process(T command);
}
When you think about just interfaces, is there any advantage over direct using? Like :
public interface ICommandProcessor
{
void Process(ICommand command);
}
Edit:
@Olivier's answer hits a good point.
It is an advantage if you want to create a command processor for a specialized command type
If specialized command type is needed, it is good. But I am not sure to force all command classes to use generics because of maybe they would need specialized command type. So, when think both things :
public interface ICommandProcessor<in T> where T : ICommand
{
void Process(T command);
}
public interface ICommandProcessor : ICommandProcessor<ICommand>
{
}
public class SampleClass : ICommandProcessor
{
public void Process(ICommand command)
{
//..
}
}
public class AnotherSampleClass : ICommandProcessor<IAnotherCommand>
{
public void Process(IAnotherCommand command)
{
//..
}
}
Can we say it is the best design for ICommandProcessor
?
By this way, classes can implement ICommandProcessor
or ICommandProcessor<T>
if needs to create special one, I think.
T
is constrained toICommand
at compile time, so if you get your concrete types wrong, you won't have to wait until your program blows up unexpectedly at run time to find that out.ICommand
.