16

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

具体来说,我正在为谷歌地图网络服务 API 设计,它具有一般网络服务请求

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

service并且output是强制参数,并且sensor是强制参数。还有一个可选参数language

每个服务都有一组强制和可选参数。地理编码服务有两个可选参数,boundsregion. 它还具有相互排斥的强制参数,addresslocation,指定服务类型(分别为直接或反向地理编码)。我用新的儿童班来代表这种相互排斥。

我想像这样的类层次结构:

  .-----.
  | 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();

如何创建一个使用其插入的类和超类中的参数和方法的 Builder?

4

1 回答 1

19

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

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

它确保了类型安全并避免了由于未初始化的强制参数或拼写错误而引发的任何异常,以及漂亮的流畅界面。然而,将这样一个简单的结构表示为 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...
}

地理编码请求网址:

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...
}
于 2012-06-07T23:57:43.433 回答