I am from .NET world, but now I am reading a book, where all examples are written in Java. Here is code snippet:
public class Order {
static class Builder {
private String security;
private int quantity;
public Builder() {}
public Builder buy(int quantity, String security) {
this.security = security;
this.quantity = quantity;
}
// some other builder pattern methods...
public Order build() {
return new Order(this);
}
}
private final String security;
private final int quantity;
private Order(Builder b) {
security = b.security;
quantity = b.quantity;
}
}
So if someone can explain me: 1. How can we have static class with non-static fields? 2. The author of the book is writing such thing: By using the builders as the mutable object, you ensure the immutability of the Order
data members for easier concurrency.
Can someone give me an example, how it can actually simplify concurrency issues as soon as we still have mutable builder object, which would have the same concurrency issues, the same way we would have mutable Order
.
Aucun commentaire:
Enregistrer un commentaire