Posts

Showing posts from March, 2013

Fickle - variables with attached events (in Java)

In programming a personal project I came across the requirement to have collections of objects that all have access to the same value. Another related requirement was that if the value changed then those other classes may have to reconfigure themselves (a knock-on effect due to that original value changing). The obvious answer was to employ an event driven design. I created a class to handle these requirements in a generic way. I thought of this new "value class" as containing a capricious value, and so called it Fickle, which is a parameterized class. It is not synchronised, so beware when multithreading. Here is the code: import java . util . Collections ; import java . util . Set ; import java . util . WeakHashMap ; public class Fickle<T> { private T _value ; private final Set < OnChangeListener < T > > _listeners ; public Fickle ( final T value ) { _value = value ; _listeners = Collections . n...