我想将Web服务URL请求表示为一个对象,并发现有许多常见参数可以在继承层次结构中“上升”。 请求可能有许多参数,一些是必需的,其他是可选的。对此,我认为Bloch的Builder模式是一个不错的选择,它使用流畅接口来模拟命名参数。
具体而言,我正在设计适用于Google Maps Web服务API的通用Web服务请求。
具体而言,我正在设计适用于Google Maps Web服务API的通用Web服务请求。
http://maps.googleapis.com/maps/api/service/output?{parameters}
service
和output
是必填的参数,sensor
是必填的参数。还有一个可选的参数language
。
每个服务都有一组必填和可选的参数。地理编码服务有两个可选参数bounds
和region
。它还具有互斥的必填参数address
或location
,它们分别指定了服务类型(直接或反向地理编码)。我使用新的子类表示此相互排斥。
我将类层次结构设想如下:
.-----.
| 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),能够使用它所插入的类和超类中的参数和方法?
getThis()
的重写实现不应该是抽象的。 - Eric Tobiasprotected RequestUrl(Builder builder)
method will always complain forRaw use of parameterized class 'Builder'
- Ghilteras