Content deleted Content added
HagermanBot (talk | contribs) m 61.14.96.7 didn't sign: "Ambiguity? Or am I just confused..." |
No edit summary |
||
Line 1:
==Builders and immutable objects==
A major benefit of builders is that they can be used to create immutable objects without complex constructors. In Java, the builder pattern also simulates named constructor parameters:
<source lang="java">
public final class Pizza {
private final String dough;
private final String sauce;
private final String topping;
private Pizza(Builder builder) {
dough = builder.dough;
sauce = builder.sauce;
topping = builder.topping;
}
public static class Builder {
private String dough;
private String sauce;
private String topping;
public void dough(String dough) {
this.dough = dough;
}
public void sauce(String sauce) {
this.sauce = sauce;
}
public void topping(String topping) {
this.topping = topping;
}
public Pizza create() {
return new Pizza(dough, sauce, topping);
}
}
}
/** A customer ordering a pizza. */
class BuilderExample {
public static void main(String[] args) {
Pizza hawaiian = new PizzaBuilder()
.dough("cross");
.sauce("mild");
.topping("ham+pineapple")
.create();
}
}
</source>
The Sequence diagram dosen't seems to correspond to the example in Java any toughts ? --[[User:Khalid hassani|Khalid hassani]] 11:15, 11 August 2006 (UTC)
==Correspondance between the sequence diagram and the Java program==
|