I have this inteface:
public interface ICommand
{
bool Execute(Vector2 commandPosition);
bool Execute(GameUnit gameUnit);
bool Execute(string shortcut);
}
And a class with these methods making same things with different argument types
private void DispatchGameUnitCommand(GameUnit gameUnit)
{
if (_preparedCommand != null)
{
_preparedCommand.Execute(gameUnit);
return;
}
for (int i = 0; i < _commands.Length; i++)
{
if (_commands[i].Execute(gameUnit))
{
return;
}
}
}
private void DispatchLocationCommand(Vector2 screenPosition)
{
if (_preparedCommand != null)
{
_preparedCommand.Execute(screenPosition);
return;
}
for (int i = 0; i < _commands.Length; i++)
{
if (_commands[i].Execute(screenPosition))
{
return;
}
}
}
private void DispatchShortcutCommand(string shortcut)
{
if (_preparedCommand != null)
{
_preparedCommand.Execute(shortcut);
return;
}
for (int i = 0; i < _commands.Length; i++)
{
if (_commands[i].Execute(shortcut))
{
return;
}
}
}
How could I improve them removing duplicated code? Is it possible in anyways?