Thursday, October 06, 2005

Dealing easily with immutable objects

Immutable object are a serious design improvement when used on value objects. I mean by 'value object', a business object which has no identity as 'ProductDescription', 'Adress', 'Money',... . The reason of the improvement is very well explained in Eric Evans book : Domain Design Driven. In a few words, immutable objects avoid side effect, simplify design and need not to be cloned.
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.

16 comments:

Anonymous said...

Interesting Blog. Thanks.

Sincerely,

Pat
machinist

Anonymous said...

Hi people
I do not know what to give for Christmas of the to friends, advise something ....

Anonymous said...

Hello. Good day
Who listens to what music?
I Love songs Justin Timberlake and Paris Hilton

Anonymous said...

car insurance uk
car insurance rats
compare car insurance
online auto insurance quote
car insurance rats
budget car insurance
buy car insurance
free online car insurance quote
in car insurance
infinity car insurance
car insurance group
car insurance coverage
auto insurance company
car insurance for woman
cheap car insurance rate
car insurance price
cheap car insurance uk
cheapest car insurance
cheap car insurance rate
car insurance quote
discount car insurance
online car insurance rate
collector car insurance
car insurance los angeles
low car insurance
car insurance los angeles
agent car company home insurance life quote rate
car insurance dallas
cheap car insurance online
car insurance for woman

http://cheap-car-insurance.quickfreehost.com

Random Keyword: :)
car insurance price

Anonymous said...

Hello. Alone on Valentine's Day? [url=http://lovefinder8.tripod.coml]Live adult chat[/url] Find sex partner in your area! Free offer
for fun

Anonymous said...

Hi, it is really nice resource. I think, i'll see this site again.
- www.blogger.com c
http://mosquitoringtone1.4x2.net mosquito ringtone
http://freenokiaringtone1.4x2.net free nokia ringtone
http://freeverizonringtone1.4x2.net free verizon ringtone
http://mp3ringtone1.4x2.net mp3 ringtone
http://nokiaringtone1.4x2.net nokia ringtone
http://freemotorolaringtone1.4x2.net free motorola ringtone
http://freemp3ringtone1.4x2.net free mp3 ringtone
http://samsungringtone1.4x2.net samsung ringtone
http://freepolyphonicringtone1.4x2.net free polyphonic ringtone
http://britneyspearsringtone1.4x2.net britney spears ringtone

Anonymous said...

Hey,

What is it with girls fighting?

BigMike


[url=gross-videos.com]gross-videos.com[/url]

Anonymous said...

uk alternative viagra super viagra pro viagra women uk alternative viagra viagra shelf life buy online viagra viagra benefits viagra 34434 suppliers of viagra buy viagra in england free viagra in the uk cheap generic viagra over the counter viagra viagra online uk

Anonymous said...

Can anyone recommend the top performing Remote Desktop program for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central software deployment
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!

Anonymous said...

Prodlessusedy, clomid online ovennaWoobe, [url=http://www.webjam.com/clomidonline]buy clomid[/url] carmarbiffDap
23

Anonymous said...

I wish not agree on it. I over nice post. Specially the title-deed attracted me to read the intact story.

Anonymous said...

Genial brief and this mail helped me alot in my college assignement. Gratefulness you seeking your information.

Anonymous said...

On the repayment date the online cash advance amount and the interest rate will. Reply to this 11262009 153 AM faxless payday loans canada wrote payday. 1000 advance cash loan payday alaska cash advance payday loans payday loans. Cash advance network is a rip off. [url=http://viiumft.rahuketu.net/map.html]visa cash advance[/url] First American Cash Advance Best Military Loan Sites We39ve researched the. All payday loan institutions do not offer military cash loan Following this.

Anonymous said...

chararorylarp, buy zyrtec, Carardykats, [url=http://www.webjam.com/zyrteconline]zyrtec[/url], Glaccerve

Anonymous said...

http://markonzo.edu births http://cipro.indieword.com/

Anonymous said...

Battery Powered Fan For Baby