Will this be a good argument for polymorphism

Sorry if this is really a basic question, but I am struggling with how I should attack this. I am trying to complete some commands for an OLE object, the basic spec looks like this:

Set Window window_id
    //Units is part of the position setter.
    [ Position ( x, y ) [ Units paper_units ] ] 
    [ Width win_width [ Units paper_units ] ] 
    [ Height win_height [ Units paper_units ] ]
    ..... this goes on for about 20+ commands, all optional.

      

If any value between [] is optional.

So I need to create a class that lets you call it " CommandBuilder " that can set methods for all these optional setters and I am doing fine with that, the main problem I am having is the ToCommandString method which is to output a string that will look like like that:

Set Window 1 Position (x,y) Units "m" Height 100 Units "m" + what ever else the user added

      

Just doing a few, as long as they are based on variables that are set and concatenated to a string, work great when there is nothing fancy about the variables being set or there are only a few variables, but when there are heaps of variables and / or nested values ​​are also optional. this can make the ToString method very long and difficult + difficult to maintain if something changes.

I was wondering if I could solve this using polymorphism by doing something like this.

interface ICommand
{
    string ToCommandString();
}

class PositionCommand : ICommand
{
    double X;
    double Y;
    string Units;

    public PositionCommand(double x, double y)
    {
        this.X = x;
        this.Y = y;
    }

    public PositionCommand(double x,double y, string units)
    {
        this.X = x;
        this.Y = y;
        this.Units = units;
    }

    public string ToCommandString()
    {
        //Add more stuff here to handle empty units.
        return String.Format(" Postion ({0},{1})", X.ToString(), Y.ToString());
    }
}
....more command classes.

      

Then all my given methods in the " CommandBuilder " can just create the correct command type, add it to the list, then the main ToString method in the " CommandBuilder " can loop through all the ones that have been set and call ToCommandString without having to worry about to do any if statuses or null checks.

Will this be the right way?

PS If you need more information I would be happy to add, just didn't want it to be at the beginning.

0


source to share


2 answers


This sounds reasonable to me. I would definitely keep the construction of ICommand instances inside the CommandBuilder:

class CommandBuilder
{
  private List<ICommand> _commands = new List<ICommand>();

  public CommandBuilder Position(double x, double y)
  {
    _commands.Add(new PositionCommand(x,y))
    return this;
  }

  ...
}

      



Instead of just

class CommandBuilder
{
  public void AddCommand(ICommand cmd)
  { ... }
}

      

+2


source


Yes. I think you got it well enough.



0


source







All Articles