这是多态性的好例子吗 [英] Would this be a good case for polymorphism

查看:78
本文介绍了这是多态性的好例子吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个非常基本的问题,但是我在努力应对这个问题上感到很挣扎.我正在尝试为OLE对象包装一些命令,基本规格如下所示:

Sorry if this is a really basic question but I struggling with how I should attack this. I am trying to wrap up some commands for a 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.

[]之间的任意内容是可选的.

Where anyhting in between [] is optional.

因此,我需要创建一个名为" CommandBuilder "的类,该类可以为所有这些可选的设置器提供设置方法,这很好,我可以处理,我遇到的主要问题是需要输出一个看起来像这样的字符串的ToCommandString方法:

So I need to create a class lets call it "CommandBuilder" that can will have set methods for all these optional setters, thats fine I can handle that, the main problem I'm having is the ToCommandString method that needs to ouput a string which would look somthing like:

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

当设置的变量没有什么复杂的情况,或者只有几个变量,但是当有大量的变量和/或嵌套的值也是可选的,它会使ToString方法变得非常长且复杂,并且如果有任何更改,则很难维护.

Just doing a few if's based on variables that get set and joining the string works fine when there is nothing complicated about the variables being set or there are only a few variables but when there are heaps of variables and/or nested values that are also optional, it can make the ToString method very long and complicated + hard to maintain if anything changes.

我想知道是否可以通过执行类似这样的操作来解决多态性问题.

I was wondering if I could solve this by 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.

然后我在" CommandBuilder "中设置的所有方法都可以创建正确的命令类型并将其添加到列表中,然后在" CommandBuilder "方法中的主要ToString可以循环遍历所有已设置的内容并调用ToCommandString,而不必担心是否执行任何语句或空检查.

Then all my set methods in "CommandBuilder" can just create the right command type add it to a list, then the main ToString in "CommandBuilder" method can loop through all the ones that have been set and call ToCommandString and no have to worry about doing any if statments or null checks.

这将是解决此问题的正确方法吗?

Would this be the correct way to go about this?

P.S.如果您需要更多信息,我很乐意补充一下,只是不想让它长途跋涉.

P.S. If you need more info I would be glad to add, just didn't want to make it to long first go.

推荐答案

对我来说,这听起来很合理.我一定要在CommandBuilder内部保留ICommand实例的构造:

That sounds reasonable to me. I would definately keep the construction of the ICommand instances internal to 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;
  }

  ...
}

不只是

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

这篇关于这是多态性的好例子吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆