6

I was just reading a Java 7 preview presentation (pdf) and there was a slide on Chained Invocation. Here is the example used in the slide:

// Construction with setters
DrinkBuilder margarita = new DrinkBuilder();
margarita.add("tequila");
margarita.add("orange liqueur");
margarita.add("lime juice");
margarita.withRocks();
margarita.withSalt();
Drink drink = margarita.drink();

// Construction with chained invocation
Drink margarita = new DrinkBuilder()
    .add("tequila")
    .add("orange liqueur")
    .add("lime juice")
    .withRocks()
    .withSalt()
    .drink();

And I have mixed feelings about this. One shouldn't chain too many method invocations into one statement. On the other hand, writing margarita.this() and margarita.that() isn't too convenient either.

Now, I am coming to Java from Delphi world. And in Delphi there is the with language construct. This is cherished by a few and loathed by many (or is it the other way around?). I find with to be more elegant than the idea of chained invocation (which I believe works on the basis of void method returning reference to object on which it has been invoked - and this is the part I don't like, as void should return nothing).

I would appreciate the with language feature being adopted by Java, so the example code could be written like so:

Drink margarita = null;
with (new DrinkBuilder()) {
    add("tequila");
    add("orange liqueur");
    add("lime juice");
    withRocks();
    withSalt();
    margarita = drink();
}

Am I the only one who would prefer this solution to the chained invocation? Anyone else feels that with could be a useful extension to Java language? (Reminds me of someone's question about the need of "Java++"...)

Harriv
  • 6,029
  • 6
  • 44
  • 76
Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
  • 2
    This is not limited to Java 7, you can write a builder by simply returning "this" in the methods. A good example is java.lang.StringBuilder. – Tim Büthe Jun 18 '09 at 10:21
  • 5
    I think the point is that Java 7 allows the chaining without returning this, therefore changing the semantics of a method call. – OregonGhost Jun 18 '09 at 10:30

6 Answers6

13

the with statement can be translated in Java using anonymous classes with initializer:

Drink margarita = new DrinkBuilder() {{
    add(“tequila”);
    add(“orange liqueur”);
    add(“lime juice”);
    withRocks();
    withSalt();
}}.drink();

the downsides of using this idiom are well documented here.

Chained Invocation is an alias for Method Chaining. That is well known idiom and works with any version of Java:

class Chained {

    public Chained withFoo() { 
        // ...
        return this;
    }

    public Chained withBar() { 
        // ...
        return this;
    }
}    

a proposal for JDK 7 is allowing of chaining method also for void return type:

class ChainedJava7 {

    public void withFoo() { 
        // ...
    }

    public void withBar() { 
        // ...
    }
}    
Community
  • 1
  • 1
dfa
  • 114,442
  • 31
  • 189
  • 228
2

This might interest you.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
2

I quite like with statements of that form but I prefer the VB version of them:

With testObject
    .Height = 100
    .Text = "Hello, World"
    .ForeColor = System.Drawing.Color.Green
End With

As each attribute in the With block still has to be preceded by a . you know that you're referencing an Object property and not, say, a local variable, reducing any namespace collisions.

If we take your example:

with (new DrinkBuilder()) {
    add(“tequila”);
    add(“orange liqueur”);
    add(“lime juice”);
    withRocks();
    withSalt();
    margarita = drink();
}

there's no easy way to tell if withSalt() is a method of DrinkBuilder or a method in local class. If you only allow methods of the with-ed object in the with block then I think they become much less useful.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • My suggestion would be withSalt() would be a method of the Drink Builder. If you found the application is not doing what it should, you could call this.withSalt(); to make it explicit what object's withSalt() method should be invoked. – Peter Perháč Jun 18 '09 at 10:44
1

I'm not a fan of this use of with; I much prefer the Python with statement. I do agree with you that void should mean void, though. In the example you provide, if a person really wants to be able to chain method invocations they should just change the return types on their methods so they're chainable.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
  • Um, it's void. As a producer you don't care to return anything, and your consumer doesn't care to receive anything (although sometimes they do). Why should you care? Semantically, nothing changes (except having to type so much). – Chris K Jun 18 '09 at 12:05
  • Except you *are* returning something, and you're doing it implicitly. If you weren't returning anything, then you couldn't invoke a method on it. Since this is Java and pretty much everything else is explicit, I'd rather the return be explicit. – Hank Gay Jun 18 '09 at 12:58
1

Maybe the many many calls to one object are the sign that some code needs to be moved around?

cadrian
  • 7,332
  • 2
  • 33
  • 42
  • 2
    No, this is part of the whole Bean mantra of Do One Thing because Java doesn't have first-class-properties so we have to write getters and setters for everything. That's why. Something else that was dropped from Java 7 (properties). – Chris K Jun 18 '09 at 12:07
  • @darthcoder - glad you mentioned that! I can't understand why this has not been part of Java since 1.0 – Peter Perháč Jun 18 '09 at 12:29
1

Joshua Bloch in Effective Java Item #2 strongly recommends the use of a Builder when you have a constructor with a lot of arguments. One reason is that it can be written to guarantee that the built object is always in a consistent state. It also avoids having complex "telescoping constructors" in the built object's class. Still another is that if you want the built object to be immutable (eg, for thread safety), it can't have setter methods.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83