Interface FlyweightPrototype<T extends FlyweightPrototype<T>>
- All Known Implementing Classes:
ByteBufferLongBigList
Flyweight copies are useful to implement multithreading on read-only (but maybe stateful) classes. An instance of a class implementing this interface is not necessarily thread safe, but it can be (thread-) safely copied many times (i.e., it can be used as a prototype). All copies will share as much as possible of the class read-only state (so they are flyweight).
In the case an implementation is stateless, it can of course return always the same singleton
instance as a copy. At the other extreme, a stateful class may decide to synchronise its
methods and return itself as a copy instead. Note that in general the object returned
by copy()
must replicate the current state of the object, not
the object state at creation time. This might require some calls to methods that
modify the class internal state: in particular, one should always check whether such
methods are pointed out in the documentation of superclasses.
Warning: if copy()
accesses mutable internal state, setters
and copy()
must be suitably synchronised.
Implementing subclasses are invited to use covariant return-type overriding to
make copy()
return the right type.
-
Method Summary
-
Method Details
-
copy
T copy()Returns a copy of this object, sharing state with this object as much as possible.- Returns:
- a copy of this object, sharing state with this object as much as possible.
-