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!

8 comments:

Felix Leipold 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.

Anonymous 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!

Sàn Đẹp said...

Loại sàn có chi phí bảo trì thấp tốt nhất là gì?
lợi ích của sàn gỗ thái lan 12mm là gì

alwaysontrip said said...

Experience the enchanting beauty of Sri Lanka with www.fabtrips.co.uk Take advantage of our affordable holiday packages to explore historical sites, vibrant wildlife, and delicious local cuisine. Whether you're seeking relaxation or adventure, Sri Lanka has it all. Don’t miss out—book your unforgettable escape today! Visit now: www.fabtrips.co.uk

Holiday Packages to Sri Lanka

Flight to Sri Lanka

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.