Thursday, January 1, 2009

Practical Java : Wrapper implementation on Collections

Wrapper implementation on Collections is good feature to know in Collections framework..

There are 3 distinct wrappers implementation on collections.
a. Synchronized Wrappers.
b. Unmodifiable Wrappers
c. Checked wrapper

Synchronized wrapper enables to add synchonization to collections. So for example if you have arrayList which is by default not threadsafe can be made Thread-Safe by using the Synchronized wrappers.

Ex: List list = Collections.synchronizedList(new ArrayList());

Note here that Collections is Class and not Collection interface.

These are normally based on Decorator pattern and since synchronized wrappers add a level of indirection which can have a high performance cost.

Next post will look more into Practical usage of Synchronized wrappers and where it should be used?



No comments: