Question

I have a one Command Interface like this,

public interface ICommand{
     public abstract Object execute(List<Inputs> inputs);
}

Now I have A command for other type of complex Executions so I came up with the new Command Interface

public interface IComplexCommand {
  public abstract Object execute(ComplexObjects obj);
}

I invoke the commands from the properties file which is done inside a static initializer block of CommandFactory. My Factory method to Invoke looks like this.

ICommand cmd= CommandFactory.getInstance().getCommand("LoopElements");
// loopElements is the key in properties file to load my com.test.my.LoopElements
// to call execute of IComplex command it will not work because I have to typecase
// this I want to avoid.

Now I have a issue like when I get the Command I do not want to typecase the Command depending on Interface but I would like to have it understood at runtime,

Can anyone please help me in better designing this. I tried to google but I was not able to get any proper answer because the question is very specific.

Was it helpful?

Solution

I would suggest not to go for any Command factories. Command pattern actually lets you parameterize your request object. So you can create different type of command object for your easy and complex command scenario, and then you can execute them based on your command type you retrive from property file. This is what i would do following command pattern, see if it helps:

public interface IOperations
{
    void PerformOperations();
}
public class EasyOperations : IOperations
{
   public void PerformOperations()
    {
        Console.WriteLine("Do easy operations here");
    }
}
public class ComplexOperations : IOperations
{
    public void PerformOperations()
    {
        Console.WriteLine("Do complex operations here");
    }
}

public interface ICommand
{
    void Execute();
}

public class EasyCommand : ICommand
{
    IOperations opn;
    public EasyCommand(IOperations opn)
    {
        this.opn=opn;
    }
    public void Execute()
    {
        opn.PerformOperations();
    }
}

 public class ComplexCommand : ICommand
{
    IOperations opn;
    public ComplexCommand(IOperations opn)
    {
        this.opn=opn;
    }
    public void Execute()
    {
        opn.PerformOperations();
    }
}   

public class OperationsPerformer
{
    IDictionary<string, ICommand> commands = new Dictionary<string, ICommand>();
    public OperationsPerformer()
    {
        commands.Add("easy", new EasyCommand(new EasyOperations()));
        commands.Add("complex",new ComplexCommand(new ComplexOperations()));
    }
    public void perform(string key)
    {
        if (commands[key] != null)
        {
            ICommand command = commands[key];
            command.Execute();
        }            
    }

}


public class Client
{
    public static void Main(String[] args)
    {
        OperationsPerformer performer = new OperationsPerformer();
        performer.perform("easy");
        performer.perform("complex");
        Console.ReadKey();
    }
}

output:

Do easy operations here Do complex operations here

OTHER TIPS

you could design it this way:

public interface ICommand
{
    public Object execute();
}

public class Command : ICommand
{
    List<Input> inputs;

    public void setInputs(List<Input> inputs)
    {
        this.inputs = inputs;
    }

    public Object execute()
    {
        // do something
    }
}

public class ComplexCommand : ICommand
{
    ComplexObjects inputs;

    public void setInputs(ComplexObjects inputs)
    {
        this.inputs = inputs;
    }

    public Object execute()
    {
        // do something
    }
}

Objects of both classes can be treated as ICommand objects so client code can call execute() and does not need to care for the concrete class behind the interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top