简体   繁体   中英

How to create a top-down overload constructor in java

I am currently working on an android project, where I want to create an object with a dynamic number of parameters.

A RequestOperation object will always have the attribute Type and shall be able to be created with 0 to 3 optional string attributes, which are stored in an ArrayList<String> attribute called requestParams .

When writing the constructors the following idea came into my mind:

Is it possible to create the constructors "pesudo-recursive", so that the constructor method, that takes three strings, adds the 3rd one to the object and calls the constructor, that takes two strings. This one would add the 2nd one to the object and then call the constructor that takes one string, which would do the same, and then passes the object with the filled requestParams arraylist to the constructor that takes no strings and just adds the Type attribute.

That would save a lazy programmer from writing (in fact long) constructors over and over again.

Here's how I imagined that (strongly simplified):

public class RequestOperation {

    public enum Type {
        //enum values and mappings
    }

    private String applicationUrl;
    private ArrayList<String> requestParams = new ArrayList<>();

    public RequestOperation(Type operationType) {
        this.applicationUrl = composeUrl(operationType, requestParams);
    }

    public RequestOperation(Type operationType, String paramFirst) {
        this.requestParams.add(0, paramFirst);
        new RequestOperation(operationType);
}

    public RequestOperation(Type operationType, String paramFirst, String paramSecond) {
        this.requestParams.add(0, paramSecond);
        new RequestOperation(operationType, paramFirst);
    }

    public RequestOperation(Type operationType, String paramFirst, String paramSecond, String paramThird) {
        this.requestParams.add(0, paramThird);
        new RequestOperation(operationType, paramFirst, paramSecond);
    }

With this approach I'm afraid, that I would create a new object inside the constructors and the params would get written to the wrong objects. Is there by any chance a way to pass the object itself to be the target of the following constructor call? If this is a bad approach at all, please let me know, i am still learning, how to program properly.

You could use varargs in a form such as:

public RequestOperation(Type operationType, String... parameters) {
    for(String param : parameters) {
       requestParams.add(param);
   }
}

This has the added benefit that you can also add as many request parameters as you wish.

EDIT: as mentioned in the comments by PPartisan an even more concise way would be

public RequestOperation(Type operationType, String... parameters) {
   requestParams = Arrays.asList(parameters);
} 

Probably it would be a better approach to define a static method for the class RequestOperation called like newInstance which takes as parameters the Type object and a List<String> and then recursively calls itself until the length of the List is 0, then it returns the RequestOperation created with the constructor that takes the Type object.

I'd say it's a better approach than the one you described because with your method you're creating a new RequestOperation object each recursive call, I think it's not a very good practice.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM