Their drawback is that it can be very unconvenient to use especially with objects containig a great amount of properties (need to pass every parameters on the constructor).
Anyway, i want introduce a kind of builder pattern that let immutable objects be very convenient to use. The principle is to embed an inner builder class in your immutable object class. Let's see an exemple :
/**
* A class for an immutable object.
*/
public class ImmutableObject {
private String coco;
private int i;
private String loala;
private double lili;
private Date date;
/**
* Constructor used by the build.
* The only requirement for use this pattern
* is that the immutable object
* need a default constructor (which can be private).
*/
private ImmutableObject() {
super();
}
public String getCoco() {
return this.coco;
}
public int getI() {
return this.i;
}
public double getLili() {
return this.lili;
}
public String getLoala() {
return this.loala;
}
public Date getDate() {
return this.date;
}
//------------------- The immutable object builder
//------------------- pattern code start here
//------------------- All the code can easily be generated
//------------------- automatically by an IDE
/**
* Builder class to instanciate new Immutable object
* in a convenient way.
* This Inner class can easily be generated automatically
* by an IDE.
*/
public static class Builder extends ImmutableObject {
private ImmutableObject immutableObject;
/**
* Construct a Builder to create immutableObject.
*/
public Builder() {
super();
this.immutableObject = new ImmutableObject();
}
/**
* Construct a Builder to create a new immutable
* object from a prototype
* (usefull for edition purpose).
* @param immutableObject
*/
public Builder(ImmutableObject prototype) {
this();
this.immutableObject.coco = prototype.coco;
this.immutableObject.date = prototype.date;
this.immutableObject.lili = prototype.lili;
this.immutableObject.loala = prototype.loala;
this.immutableObject.i = prototype.i;
}
/**
* Build the ImmutableObject with defined properties.
* All previously set properties are reset.
* @return A
ImmuatbleObject
.*/
public ImmutableObject build() {
ImmutableObject result = this.immutableObject;
this.immutableObject = new ImmutableObject();
return result;
}
// setable properties
public Builder setCoco(String coco) {
this.immutableObject.coco = coco;
return this;
}
public Builder setI(int i) {
this.immutableObject.i = i;
return this;
}
public Builder setLili(double lili) {
this.immutableObject.lili = lili;
return this;
}
public Builder setLoala(String loala) {
this.immutableObject.loala = loala;
return this;
}
public Builder setDate(Date date) {
this.immutableObject.date = date;
return this;
}
}
}
The user code result in :
ImmutableObject immutable = new ImmutableObject.Builder()
.setCoco("coco")
.setI(2)
.setLili(32)
.setLoala("loala")
.setDate(new Date())
.build();
ImmutableObject im2 = new ImmutableObject.Builder(immutable)
.setI(3)
.build();
This code instanciate two ImmutableObject. The second object is a copy of the first one,
but with a different i value set on it.
The important points are :
- this pattern is not intrusive for the value object main class
- the pattern code can (and should) be generated by the IDE.
- the strong typing is kept.
Unfortunately, I never heard about such an Eclipse plugin and I have no much time to learn about Eclipse plugin developement, even if i think that's not complicated.