1

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.

4

2 回答 2

2

我建议不要去任何命令工厂。命令模式实际上允许您参数化您的请求对象。因此,您可以为简单和复杂的命令场景创建不同类型的命令对象,然后您可以根据从属性文件中检索的命令类型执行它们。这就是我将按照命令模式执行的操作,看看它是否有帮助:

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();
    }
}

输出:

在这里做简单的操作在这里做复杂的操作

于 2013-08-29T12:27:21.260 回答
0

你可以这样设计:

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
    }
}

这两个类的对象都可以被视为ICommand对象,因此客户端代码可以调用execute()并且不需要关心接口后面的具体类。

于 2013-08-29T11:42:16.810 回答