具有继承性的生成器模式

2022-09-03 06:40:13

我想将 Web 服务 URL 请求表示为对象,并发现在继承层次结构中有很多常见的参数可以“冒泡”。一个请求可以有很多参数,一些是强制性的,有些是可选的,为此,我相信Bloch的Builder模式是一个不错的选择,它用流畅的界面模拟命名参数。

具体来说,我正在为Google Maps Web Service API进行设计,该API具有一般的Web服务请求。

http://maps.googleapis.com/maps/api/service/output?{parameters}

service并且是必需参数,并且是必需参数。还有一个可选参数 。outputsensorlanguage

每个服务都有其一组必需参数和可选参数。地理编码服务具有两个可选参数和 。它还具有相互排斥的必需参数,或 指定服务类型(分别为直接或反向地理编码)。我代表了这种与新儿童班的相互排斥。boundsregionaddresslocation

我想象类层次结构是这样的:

  .-----.
  | Url |
  '-----'
     ^
     |
.---------.
| Request |
'---------'
     ^
     |----------------------------+--------------...
.---------.                 .------------.
| Geocode |                 | Directions |
'---------'                 '------------'
     ^                            ^
     |------------+               .
 .--------.  .---------.          .
 | Direct |  | Reverse |          .
 '--------'  '---------'

然后,我想做如下操作:

String output = "xml";
boolean sensor = true;
String address = "Av. Paulista, São Paulo, Brasil";
Bounds bounds  = new Bounds(-20, -10, -25, -20); //Geographic rectangle
String region  = "br";
String lang    = "pt-BR";
Coord location = new Coord(-12,-22);

DirectGeocodeRequestUrl direct = 
    new DirectGeocodeRequestUrl.Builder(output, sensor, address)
                               .bounds(bounds)
                               .language(lang)
                               .build();

ReverseGeocodeRequestUrl reverse = 
    new ReverseGeocodeRequestUrl.Builder(output, sensor, location)
                                .language(lang)
                                .region(region)
                                .build();

如何创建使用插入它的类和超类中的参数和方法的生成器?


答案 1

我的答案建立在 https://stackoverflow.com/a/9138629/946814 的基础上,但考虑到这个多层次的层次结构。

我们需要的是使用 Builder 内部类复制相同的层次结构。由于我们想要方法链接,因此我们需要一个返回层次结构的叶对象的方法。为了将其类型向上传递到层次结构中,父类具有泛型 ,并且叶与自身绑定。getThis()TT

它确保了类型安全,并避免了由于未初始化的强制参数或拼写错误而导致的任何异常抛出,再加上漂亮的流畅界面。但是,要表示像URL这样简单的结构是一个非常昂贵和复杂的设计。我希望它对某人有用 - 我更喜欢最后的字符串串联。

请求网址:

public abstract class RequestUrl{
    public static abstract class Builder<T extends Builder<T>>{
        protected String output;
        protected boolean sensor;
        //Optional parameters can have default values
        protected String lang = "en"; 

        public Builder(String output, boolean sensor){
            this.output = output;
            this.sensor = sensor;
        }

        public T lang(String lang){
            this.lang = lang;
            return getThis();
        }

        public abstract T getThis();
    }

    final private String output;
    final private boolean sensor;
    final private String lang;

    protected <T extends Builder<T>> RequestUrl(Builder<T> builder){
        this.output = builder.output;
        this.sensor = builder.sensor;
        this.lang = builder.lang;
    }

    // other logic...
}

GeocodeRequestUrl:

public abstract class GeocodeRequestUrl extends RequestUrl {
    public static abstract class Builder<T extends Builder<T>>
        extends RequestUrl.Builder<Builder<T>>{

        protected Bounds bounds;
        protected String region = "us";

        public Builder(String output, boolean sensor){
            super( output, sensor );
        }

        public T bounds(Bounds bounds){
            this.bounds = bounds;
            return getThis();
        }

        public T region(String region){
            this.region = region;
            return getThis();
        }

        @Override
        public abstract T getThis();
    }

    final private Bounds bounds;
    final private String region;

    protected <T extends Builder<T>> GeocodeRequestUrl(Builder<T> builder){
        super (builder);
        this.bounds = builder.bounds;
        this.region = builder.region;
    }

    // other logic...
}

DirectGeocodeRequestUrl:

public class DirectGeocodeRequestUrl extends GeocodeRequestUrl {
    public static class Builder<Builder>
        extends GeocodeRequestUrl.Builder<Builder>{

        protected String address;

        public Builder(String output, boolean sensor, String address){
            super( output, sensor );
            this.address = address;
        }

        @Override
        public Builder getThis(){
            return this;
        }

        public DirectGeocodeRequestUrl build(){
            return new DirectGeocodeRequestUrl(this);
        }
    }

    final private String address;

    protected DirectGeocodeRequestUrl(Builder builder){
        super (builder);
        this.address = builder.address;
    }

    // other logic...
}

ReverseGeocodeRequestUrl:

public class ReverseGeocodeRequestUrl extends GeocodeRequestUrl {
    public static class Builder<Builder>
        extends GeocodeRequestUrl.Builder<Builder>{

        protected Coord location;

        public Builder(String output, boolean sensor, Coord location){
            super( output, sensor );
            this.location = location;
        }

        @Override
        public Builder getThis(){
            return this;
        }

        public ReverseGeocodeRequestUrl build(){
            return new ReverseGeocodeRequestUrl(this);
        }
    }

    final private Coord location;

    protected ReverseGeocodeRequestUrl(Builder builder){
        super (builder);
        this.location = builder.location;
    }

    // other logic...
}

答案 2

推荐