Take the 2-minute tour ×

I've encountered a very nasty i would say 'side effect', but it's obviously a problem of bad design. I'm using Guava ForwardingList pattern to decorate a regular List. My purpose is to build a size-limiting List where oldest elements are kicked out when a maximumSize is met(a simple FIFO design). Note that i don't wont to proxy or clone my existing collection. But i have this very nasty side effect:

List<String> originalList = new ArrayList<String>();
int maximumSize = 2;


System.out.println(originalList); // [foo, bar]

ListFactory<String> factory = ListFactory.getInstance(String.class);
List<String> decoratedList = factory.newTalendList(originalList, maximumSize);

System.out.println(originalList); // [bar, beer]

System.out.println(originalList); // [bar, beer, ben] <-- !!!
System.out.println(decoratedList); // [bar, beer, ben] <-- !!!

(note: my decorated class overrides add() to remove the first element of the list upon adding a new one. All other non-overridden methods, included toString(), are delegated to original List)

Ok, as you may see if i add an element using the original add() method i can exceed the maximumsize...ok, i suppose it's unavoidable (and that's not wrong by design, after all). But this is not true-by-design for decoratedList.

The only workaround i found was something like:

List<String> decoratedList = factory.newTalendList(new ArrayList<String>(originalList), maximumSize);

but it doesn't seems the best way (and i'm not sure it works in every circumnstances): i'm not decorating originalList, but her anonymous clone! I'm wondering: perhaps have I completely messed with my design? Is there a better way to build it?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

The only way this pattern is going to work is if you never reference originalList again after you've created the decorator. The ForwardingList couldn't possibly have any control over what happens in the originalList. (No decorator could.)

What you should probably do, overall, is to create a factory method that returns a completely new decorated list and never lets you access the original list at all.

share|improve this answer
+1 this definitively works :) tnx for help! –  Gabriele B Dec 17 '12 at 21:41

What you try to do is very weird. Original list knows nothing about being constrained, also constrained list believes he is only who uses original list. So just don't use original list in a way that may mess its decorated version.

share|improve this answer

Your Answer


