1

I would like to ask how to handle with multiple constructors.

if(a != null && b != null)
    return new QueryProducer(query, a, b);
else if(a != null)
    return new QueryProducer(query, a);
else if(b != null)
    return new QueryProducer(query, b);
else return new QueryProducer(query);

I would like to avoid complex if else blocks. Scalability is also not very good in this case.

2
  • 3
    Use one constructor, have the null checks inside the constructor. Commented Sep 28, 2013 at 11:30
  • Why are you using multiple constructors in a central place like this? When I find myself doing this I question whether I have centralized a decision that should not be centralized at this particular place in the source code. It would be more natural to at those places where a and b are known and available to use the constructor that uses a and b. Commented Sep 28, 2013 at 11:35

3 Answers 3

8

How about using builder pattern here? See this link from Javacodegeeks for code example

QueryProducer.withQuery(yourQuery).withA(a).withB(b).build();
Sign up to request clarification or add additional context in comments.

3 Comments

Great suggestion. Your idiom is almost complete, except that as written it will not work as a builder. You would need QueryProducer.BuildWithQuery(yourQuery).withA(a).withB(b).build();
@scottb updated. I've seen builder pattern used with and without the build(or equivalent) at the end, though here I think it is good to have. Should it be obvious from the class name that it is a builder, I'd perhaps not include it in the end.
without a build() method, then you don't really have a JavaBean-style builder. Without it, you would need to specify to the API user that the builder methods must always be called in a particular order (or that one particular method always be called last). This is a valid idiom, of course, but the JavaBean style is less restrictive, if a bit more verbose.
0

Create a builder that builds a QueryProducer based on the parameters passed. You can put all the logic for creating the object in one place. The other thing that comes to my mind is passing all three parameters to the constructor, and handling it there, but I almost always avoid putting any logic in the constructor because of the exceptions that can occur.

1 Comment

The absolute best place to enforce invariants is in the constructor. This is particularly true if the application is to be concurrent, as invariants are best checked after any defensive copies (if needed) have been made.
0

As others have answered, if you have a situation in which any of the following are true, then you should strongly consider using the Builder Pattern (using a helper object -- usually defined by a static nested class in your object's class -- to construct the object you want):

  • Do I have 4 or more parameters for my constructor?
  • Am I overloading constructors?
  • Does my constructor have parameters that specify data that is optional?
  • Do I expect to add more parameters to my constructor at a future date?
  • Do I want the readability of the JavaBean pattern while still being able to construct immutable objects?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.