I’ve been looking for a flyweight pattern implementation and gave up after reaching page 20 of Google search. While there are countless stupid examples out there, it seems no one has ever published are reusable implementation in Java.
For me, flyweight really only makes sense if you have to keep many such instances, so it has to be implemented as a collection. What I would like is a Factory that takes a byte/short/int/long mapper implementation, and returns either a List, Set or Map, that looks like a normal Object collection, but stores it’s data internally as an array of primitive, thereby saving lots of ram. The mapper would take an object of type X, and map it to a primitive, or do it the other way around.
Is there something like that available somewhere?
[EDIT] I am looking for a Collection library that support this pattern, not just any example, of which there are hundreds.
If you want to replace List, you can use TByteArrayList instead. If youw ant to replace List where MyClass { int a; T object; } you can use TIntObjectHashMap instead.
If you want to replace something with two fields which must be ordered or three or more fields, you need to implement your own class which wraps arrays to hold the data. This is using a column based table model.
e.g.
From Java 5.0, the auto-boxing caches are examples of flyweights.
If you want to read the code, have a look at Integer.valueOf(int) in your IDE or
http://www.docjar.com/html/api/java/lang/Integer.java.html line 638
EDIT:
Autoboxing for Integer uses IntegerCache which is a collection. An ArrayList is a class which wraps an array and has a size…