我想將一個Web服務URL請求表示爲一個對象,並發現有很多通用參數可能會在繼承層次結構中「冒泡」。一個請求可能有很多參數,一些必需的和其他可選的,我相信Bloch的Builder模式是一個不錯的選擇,它用流暢的接口模擬命名參數。具有繼承功能的構建模式
具體來說,我設計了谷歌地圖的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();
如何創建一個使用的參數和方法,從生成器它所插入的類和超類?
在具體類中重寫'getThis()'的實現不應該是抽象的。 – Eric
@EricTobias你是對的,有複製粘貼的危險。固定。 –
這太神奇了!正是我在找什麼! – Maddy