Wednesday, 9 December 2009

Immutable Wrapper

I am a huge fan of immutables, especially for value objects. Even in clumsy static languages like C# and Java that seem to throw every obstacle in your way to deter you from using them I find the benefits outweigh the costs.

Sometimes though a mutable makes sense. Whether that's because of the restrictiveness of a framework or the paradigm of the language. But what if we don't want to loose the power of immutables behind the scenes?

Introducing The Immutable Wrapper Pattern!

The Immutable Wrapper is a wonderfully simple pattern because there are only two things you need to do.
  1. Create a class with only one mutable field
  2. Wrap the immutables functions.
Here's an example. You've got a copy-on-write list implementation but your binding it to some GUI control that gives you a big headache if you try and point it to a different data source.

Here's your immutable:
  class CopyOnWriteList
{
private readonly IEnumerable items;

CopyOnWriteList Add(item)
{
return new CopyOnWriteList(new List(items, item));
}
}
Now wrap it:
  class WrappedCopyOnWriteList
{
CopyOnWriteList list;

void Add(item)
{
list = list.Add(item)
}
}
There, it couldn't be simpler. So simple in fact I don't know what else to say. Enjoy!

6 comments:

felix said...

That reminds me of the idea of a ValueModel in the Visual Works UI framework.
It is a bit simpler and more generic and offers change notification. Essentially it has a void setValue(value), and a Object getValue() method, plus a method to register listeners.

The benefits of this indirection (in UI terms) go beyond being able to use immutable value objects. You can even change the underlying object, for example if a textbox should bind to a field of an object that can be selected in listbox.

Also, I think for entities it is quite appropriate to use mutable objects.

Pramod Negi said...

Nice blog. Interesting information.
This looks like a very nice place! I liked it so much and very interesting, too! Thanks for sharing the experience.
Cheap Flights to Penang
Flights to Penang
penang Flights to

radhe said...

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. Very informative content and I enjoyed reading your blog keep it up.
istanbul Holiday Packages
Cheap istanbul Holidays
istanbul Holidays

Veer said...

Nice blog. Interesting information.
This looks like a very nice place! I liked it so much and very interesting, too! Thanks for sharing the experience.
malaysia Holiday Packages
Cheap malaysia Holidays

diseƱar cocina said...

Great taking the time to build clear the terminlogy into the inexperienced persons!

paranoia said...

I am really impressed. Today I spent a lot of time searching for something interesting on this topic. Finally I found your site. Thanks for that!

About Me

My photo
West Malling, Kent, United Kingdom
I am a ThoughtWorker and general Memeologist living in the UK. I have worked in IT since 2000 on many projects from public facing websites in media and e-commerce to rich-client banking applications and corporate intranets. I am passionate and committed to making IT a better world.