0

I have this ICommand interface.

public interface ICommand 
{
    public void PerformUnitCommand(ICommandParameters params,UnityAction callback);
}

I want to extend classes with methods as such, with varying parameters so I tried to implement it like the following:

public class MoveToDestination : ICommand 
{
    public void PerformUnitCommand(ICommandParameters params, UnityAction callback) { ... }
}

public class Attack : ICommand
{
    public void PerformUnitCommand(ICommandParameters params, UnityAction callback) { ... }
}

public class Harvest : ICommand
{
    public void PerformUnitCommand(ICommandParameters params, UnityAction callback) { ... }
}
public class ICommandParameters
{
    public Vector3 Destination;
    public Target target;
    public Resource targetResource;
}

What is the best practice on solving this problem? I feel like this is not what an interface is created for and I should be using a different pattern. But I also feel like my problem should be solve by the likes of interface where in my AI script I will only call PerformUnitCommand() and the AI script will not have to know about the objects inside each class. Any thoughts is greatly appreciated !

  • so, the problem is `ICommandParameters params` being "catch all" parameter that has 3 different properties that belong to 3 different actions respectively? – Bagus Tesa May 03 '22 at 05:44
  • Yes, that is what I am currently thinking but I also think if I add more class like Deliver, Heal and etc , somehow these 3 properties will be often used. Like for example Heal will need a target, Deliver will need a destination and etc. – unity noobies May 03 '22 at 05:51
  • Thought I would link this here too: https://stackoverflow.com/questions/56867/interface-vs-base-class – WQYeo May 03 '22 at 08:57
  • I was wrong in the first place. I shouldnt be using interface forcing each class to have the same behaviour. Thanks for the help guys! – unity noobies May 04 '22 at 06:25

0 Answers0