Watch out for Hibernate unsaved-value AspectJ 1.2.1 now available
Nov 05

Chaining Constructors and creating Creation Methods

Tech Add comments

Cedric has has opined on Why chaining constructors is bad.

I have always disliked the names that constructors have in Java (e.g. the same name of the class).

I now consider having one constructor (which isn’t public) and then having static creation methods which call the constructor correctly:

Person p = Person.createWithLastName(lastname);

Person p = Person.createWithFullNames(firstname, lastname);

With an example like this, it is probably overkill. But if you find yourself with many constructors, and it is hard to remember which one you should use, then it is is nice to do this.

Of course, you often refactor these methods into a Creation Factory seperately.

22 Responses to “Chaining Constructors and creating Creation Methods”

  1. Cedric Says:

    But you are using different names again. Why not an overloaded create() method?

    I can remember what parameters to pass but I will most likely not remember how you called that method (was it “withFullNames” or “withFullName”?).

  2. Dion Almaer Says:

    I prefer being able to type in my IDE:

    Person p = Person.CTRL SPACE

    and choose the method that makes sense.

    A better example is shown in “Refactoring to Patterns” which migrates:

    public class Loan … {
    public Loan(double commitment, int riskRating, Date maturity) {
    this(commitment, 0.00, riskRating, maturity, null);
    }

    public Loan(double commitment, int riskRating, Date maturity, Date expiry) {
    this(commitment, 0.00, riskRating, maturity, expiry);
    }

    public Loan(double commitment, double outstanding, int customerRating, Date maturity, Date expiry) {
    this(null, commitment, outstanding, customerRating, maturity, expiry);
    }

    public Loan(CapitalStrategy capitalStrategy, double commitment, int riskRating, Date maturity, Date expiry) {
    this(capitalStrategy, commitment, 0.00, riskRating, maturity, expiry);
    }

    public Loan(CapitalStrategy capitalStrategy, double commitment, double outstanding, int riskRating, Date maturity, Date expiry) {
    …. do the work ….
    }
    }

    to:

    public class Loan … {
    Loan(capitalStrategy, commitment, outstanding, riskRating, maturity, expiry);

    createTermLoan(commitment, riskRating, maturity);
    createTermLoan(capitalStrategy, commitment, outstanding, riskRating, maturity);
    createRevolver(commitment, outstanding, riskRating, expiry);
    createRevolver(capitalStrategy, commitment, outstanding, riskRating, maturity);
    createRCTL(commitment, outstanding, riskRating, maturity, expiry);
    createRCTL(capitalStrategy, commitment, outstanding, riskRating, maturity);
    }

  3. Valiumbuy Says:

    Better a lean jade than an empty halter.
    buy loss online pill weight [url=http://www.info.netfast.org/weight-loss-pill/site_map.html]buy loss online pill weight[/url]

  4. Valiumbuy Says:

    Better a lean jade than an empty halter.
    buy loss online pill weight [url=http://www.info.netfast.org/weight-loss-pill/site_map.html]buy loss online pill weight[/url]

  5. Valiumbuy Says:

    Better a lean jade than an empty halter.
    buy loss online pill weight [url=http://www.info.netfast.org/weight-loss-pill/site_map.html]buy loss online pill weight[/url]

  6. Valiumbuy Says:

    Be careless in your dress if you must, but keep a tidy soul.
    buy inurl online tramadol [url=http://www.info.netfast.org/buy-tramadol/site_map.html]buy inurl online tramadol[/url]

  7. Valiumbuy Says:

    Be careless in your dress if you must, but keep a tidy soul.
    buy inurl online tramadol [url=http://www.info.netfast.org/buy-tramadol/site_map.html]buy inurl online tramadol[/url]

  8. Valiumbuy Says:

    Be careless in your dress if you must, but keep a tidy soul.
    buy inurl online tramadol [url=http://www.info.netfast.org/buy-tramadol/site_map.html]buy inurl online tramadol[/url]

  9. tfwddmsk Says:

    Better one small fish than an empty dish.
    bad credit payday loan [url=http://www.info.netfast.org/payday-loan/site_map.html]bad credit payday loan[/url]

  10. tfwddmsk Says:

    Better one small fish than an empty dish.
    bad credit payday loan [url=http://www.info.netfast.org/payday-loan/site_map.html]bad credit payday loan[/url]

  11. tfwddmsk Says:

    Better one small fish than an empty dish.
    bad credit payday loan [url=http://www.info.netfast.org/payday-loan/site_map.html]bad credit payday loan[/url]

  12. Consolid Says:

    Hello, for result
    best chance!
    card consolidate credit debtconsolidate debt
    loan

  13. dumionzu Says:

    Is that… in person?
    buy prescription tramadol without [url=http://www.masthave.netfast.org/buy-tramadol/site_map.html]buy prescription tramadol without[/url]

  14. dumionzu Says:

    Is that… in person?
    buy prescription tramadol without [url=http://www.masthave.netfast.org/buy-tramadol/site_map.html]buy prescription tramadol without[/url]

  15. dumionzu Says:

    Is that… in person?
    buy prescription tramadol without [url=http://www.masthave.netfast.org/buy-tramadol/site_map.html]buy prescription tramadol without[/url]

  16. ihetkphd Says:

    Honesty is the best policy – when there is money in it.
    buy tramadol hcl [url=http://schildau.info/buy-tramadol/site_map.html]buy tramadol hcl[/url]

  17. ihetkphd Says:

    Honesty is the best policy – when there is money in it.
    buy tramadol hcl [url=http://schildau.info/buy-tramadol/site_map.html]buy tramadol hcl[/url]

  18. fryoen Says:

    7pMg5J eaxlzsysoedk, [url=http://wgddpfiuruyh.com/]wgddpfiuruyh[/url], [link=http://teslczwpajhm.com/]teslczwpajhm[/link], http://xohzpijdkjwv.com/

  19. axwjyzsp Says:

    Another man’s mind is a closed book.
    buy generic valium [url=http://www.learn.netfast.org/buy-valium/site_map.html]buy generic valium[/url]

  20. vsvgayhf Says:

    It’s been such a long time since I last saw you.
    buy generic valium [url=http://www.learn.netfast.org/buy-valium/site_map.html]buy generic valium[/url]

  21. rwfdmgdq Says:

    What’s new?
    buy switch using valium [url=http://www.pharmacys.netfast.org/buy-valium/index.html]buy switch using valium[/url]

  22. Deonte Says:

    .0s0. brown pride dana hamm buy u a drink onion booty make it rain remix http://i-shot-myself.dfoop.com/i-ishotmyself-myself-shot.html i ishotmyself myself shot http://i-shot-myself.dfoop.com/i-shot-myself-nude.html i shot myself nude http://i-shot-myself.dfoop.com/i-shot-myself-naked.html i shot myself naked http://i-shot-myself.dfoop.com/girl-i-myself-shot.html girl i myself shot 4hh7-ffj-2dce http://i-shot-myself.dfoop.com/i-myself-shot-teen.html i myself shot teen .0n0.

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?