Command pattern and delegates

I’ve been reading about the command programming pattern here and about its possible implementation in Unity here (highly recommended reading for beginners). It is opening up a world for me, but I’m struggling with how to properly delegate a command. Let me explain. Following a combination of the above articles, I have a abstract Command class:

using UnityEngine;

public abstract class Command {

	public abstract void Execute(GameObject obj);
}

And a pseudo-fire command that derives from it:

using UnityEngine;

public class CommandFire : Command {

	public override void Execute(GameObject obj)
	{
		Debug.Log("Fire " + obj.name);
	}
}

Now I have a MonoBehaviour that I can add to any gameobject (yes, I know it replicates functionality that Unity already has in the Project Settings, but this is for learning purposes):

using UnityEngine;

public class UserInput : MonoBehaviour {

	private Command commandFire = new CommandFire();

	public KeyCode forward = KeyCode.UpArrow;
	public KeyCode left = KeyCode.LeftArrow;
	public KeyCode right = KeyCode.RightArrow;
	public KeyCode fire = KeyCode.Space;

	void Update () {
		if (Input.GetKeyDown(fire))
		{
			commandFire.Execute(gameObject);
		}
	}
}

This works when I attach it to a gameobject and I undestand well what’s going on here. The piece of puzzle that I’m missing is how to delegate e.g. an object-specific Fire method to the appropriate command, so that I could add the UserInput component onto any object, even if it only implements some of the inputs (consider a turret that doesn’t move but only fires), without worrying about whether it can move or not. The only thing I can think of is, inside the Execute method of the CommandFire class, is to do a obj.GetComponent for an interface (say, IFireable), and call the method on that component. But then GetComponent will get called every time the user fires, which is silly. What is the proper way for a specific gameobject script to delegate its own implementation of a command with this design?

Thanks a lot for your help.

If you mean object specific as have a command that executes for let’s say a shotgun and one that executes for a pistol, you have basically two ways (that I can think of now):

  1. You have to create spearate commands for each.
  2. You make an internal state machine and tell the command which state it has to be in (I wouldn’t recommend that)