Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:23:40+00:00 2026-06-05T08:23:40+00:00

I want to represent an web service URL request as an object, and found

  • 0

I want to represent an web service URL request as an object, and found that there are lots of common parameters that could be “bubbled up” in an inheritance hierarchy. A request could have lots of parameters, some mandatory and other optional, for which I believe Bloch’s Builder pattern is a nice option, emulating named arguments with a fluent interface.

Specifically, I’m designing for the Google Maps web service API, that has as general web service request

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

service and output are mandatory arguments, and sensor a mandatory parameter. There is also an optional parameter language.

Each service has its set of mandatory and optional parameters.The Geocode service has two optional parameters, bounds and region. It also has mutually exclusive mandatory parameters, address or location, that specify the type of service (direct or reverse geocoding, respectively). I represent this mutual exclusion with new children classes.

I imagine the class hierarchy as such:

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

Then, I would like to do something like the following:

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

How can I create a Builder that uses arguments and methods from the class and superclasses in which it is inserted?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:23:42+00:00Added an answer on June 5, 2026 at 8:23 am

    I’m building my answer upon https://stackoverflow.com/a/9138629/946814, but considering this multi-level hierarchy.

    What we need is to replicate the same hierarchy with the Builder inner classes. As we want method chaining, we need a getThis() method that returns the leaf object of the hierarchy. In order to pass its type upward the hierarchy, the parent classes have a generic T, and the leaf binds T to itself.

    It assures type-safety and avoids any exception throwing due to uninitialized mandatory parameters or typos, plus the nice fluent interface. However, it’s a very costy and complex design to represent such a simple structure as an URL. I hope it is useful to someone – I preferred string concatenation at the end.

    RequestUrl:

    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...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex JSON object that I want represent as C# class. I
We have objects that we want to represent in stacks (think of stacking items
I want to parse a string that represent a DateTime in UTC format. My
I want to create a global object that represents the current user in Code
I have a map that represents a DB object. I want to get 'well
I want to pull a tree structured set of objects from a web service
I have a web service class diagram, I want it to show what stored
I have access to an web service that returns an XML or JSON. Since
I have some triples that represent the locations of several cities. I want to
I have a secured METRO 2.1 web service, and I want to develop a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.