Factory Method - Unknown Parameters

I am trying to use the command template for the first time, and with it I create the factory command, I follow the instructions on the multipalsight.com course, where it implements the interface for the factory, which includes the MakeCommand method.

Now my problem is that it simply passes an array of strings as arguments for this method (its a command line application), however my commands will use many arguments from different types, my plan was to use these commands to store updates for models therefore, if the application cannot connect to the services, the commands will be queued when the connection returns.

This has always been a great moment for me with common interfaces, how can I deal with many possible arguments?

My first thought was to pass the model itself with a simple string argument with the command type (Delete, Update, etc.), however, since my models do not have a common base class or interface, I remain with a similar problem.

Did I miss something basic here?

EDIT: An example of my problem was requested.

I have a CommandFactory interface per se

public interface ICommandFactory { string CommandName { get; } string Description { get; } ICommand MakeCommand( ..arguments.. ) } 

And I have simple models like (pure example)

 public class Model1 { public string Name {get;set;} public int Age {get;set;} } public class Model2 { public DateTime Time {get;set;} public double Price {get;set} } 

If I wanted to create a team that, for example, updated model1, I’m not interested in what the MakeCommand interface should look like, I can’t do MakeCommand (cmdType line, model Model1), because I have several different models that share no common base class / interface

+5
source share
3 answers

It looks like you want individual models to determine how they can be updated. In this case, you cannot pass the function / action from the model to MakeCommand?

 public class Model { public string Name {get;set;} public int Age {get;set;} public void UpdateModel() {...} } public interface ICommandFactory { string CommandName { get; } string Description { get; } ICommand MakeCommand(Action<Model>); ICommand MakeCommandFunc(Func<Model, bool>); } public class Command : ICommand { Action<Model> _action; Command(Action<Model> action) { _action = action; } Execute() { _action(); } } 

EDIT: As requested, use a common interface class to model all classes

 public interface IModel { void UpdateModel(); } public class Model1 : IModel { public string Name {get;set;} public int Age {get;set;} // implement updating of the model public void UpdateModel() {...do model update...} } public class Model2 : IModel { public DateTime Time {get;set;} public double Price {get;set} // 'dummy' implement updating of the model if this model does not supports updating public void UpdateModel() { // do nothing or throw NotImplementedException(); } } public interface ICommandFactory { string CommandName { get; } string Description { get; } ICommand MakeCommand( IModel model ); } 
+1
source

I suggest you use the command template, this template uses the receiver as an object containing arguments, in your receiver you can add a list or a dictionary of objects.

On this site you can find the source code.

link: http://www.dofactory.com/net/command-design-pattern

0
source

You can also make the ICommandFactory general interface as follows:

  public interface ICommandGeneric { void execute(); } public class CommandOnModel1 : ICommandGeneric { private Model1 model; public CommandOnModel1(Model1 model) { this.model = model; } public void execute() { System.Diagnostics.Debug.WriteLine(model.ToString()); } } public interface ICommandFactory <in ModelType> { string CommandName { get; } string Description { get; } ICommandGeneric MakeCommand(ModelType model, string parameter1); } public class Model1 { } public class Model1CommandFactory : ICommandFactory<Model1> { public string CommandName { get { return "CommandOnModel1"; } } public string Description { get { return "I do stuff on Model1"; } } public ICommandGeneric MakeCommand(Model1 model, string parameter1) { return new CommandOnModel1(model); } } 

Having said that, I'm not quite sure that you should use a factory and maybe not even a command template here.

0
source

All Articles