How to get rid of instanceof in this Builder implementation

不想你离开。 提交于 2019-12-07 05:09:52

问题


The idea

I need to create Commands. Commands can be configured with parameters. Not every command can receive the same parameters. So some have to be ignored.

I have an abstract class Command in which I've defined a Builder. By default every append parameter throws 'UnsupportedOperationException'

public abstract class Command {

   public static abstract class CommandBuilder {

        // TODO instanceof. How to do this better?
        public CommandBuilder append(Parameter p)
            throws UnsupportedOperationException {

            if (p instanceof URLParameter)
                return append((URLParameter) p);

            if (p instanceof ActionParameter)
                return append((ActionParameter) p);

            if (p instanceof RepeatParameter)
                return append((RepeatParameter) p);

            if (p instanceof TimeOutParameter)
                return append((TimeOutParameter) p);

            return this;

        }

        public CommandBuilder append(URLParameter p)
                throws UnsupportedOperationException {

                    throw new UnsupportedOperationException(
                        "URLParameter not applicable");

        }

        public CommandBuilder append(RepeatParameter p)
            throws UnsupportedOperationException {

                throw new UnsupportedOperationException(
                    "RepeatParameter not applicable");

            }
            ...

}

If you want a parameter to be applicable onto a certain concrete Command, lets say an FTPCommand.

You will have to do something like this:

public class FTPCommand extends Command {

    public static class Builder extends CommandBuilder {

    @Override
    public CommandBuilder append(URLParameter p) {
            System.out.println("URLParemeter appended");
                return this;
            }
        }

}

So that when a URLParameter is provided it doesn't throw an exception anymore but instead applies it.

But the client of this CommandBuilder might not be able to provide the concrete subclass. So in general a "Parameter" is given. But it needs to go to the right place (method)

Like a URLParameter must arrive at the append(UrlParameter p)

How can I do this in a clean(er) and nice(r) way? Because I'm not really 'enthousiastic' to use instanceof .


回答1:


This looks like a classic double-dispatch or visitor scenario. From the double-dispatch reference:

a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call

The Parameter and the CommandBuilder need to interact between themselves on what to do.

The CommandBuilder can call back on the parameter. The Parameter objects all implement a common interface and the implementation of each subclass will differ.

public CommandBuilder append(Parameter p) {
   // the append method called depends on the underlying type of 'p'
   p.append(this);
}



回答2:


I would add a visitor method to your interface

interface Parameter {
    public void append(CommandBuilder builder);
}

class CommandBuilder {
    public CommandBuilder append(Parameter p) {
       p.append(this);
    }
}


来源:https://stackoverflow.com/questions/11792950/how-to-get-rid-of-instanceof-in-this-builder-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!