Class ALcdWeakModelListener<T>
- Type Parameters:
T
- The type of the short-lived object
- All Implemented Interfaces:
ILcdModelListener
,EventListener
An ILcdModelListener
implementation which only keeps a WeakReference
to the object it
wants to update when model changes are received. This avoids memory leaks, even without removing the
listener.
As an example, suppose we have a table that shows the contents of some model. Assume the table is created in a dialog that a user can show and dismiss at will. Here's the code:
ILcdModel longLiving = ...;
JTable shortLiving = ...;
longLiving.addModelListener(new ModelContentsListener(shortLiving));
private static final class ModelContentsListener extends ALcdWeakModelListener<JTable> {
public ModelContentsListener(JTable aToModify) { super(aToModify); }
@Override
protected void modelChangedImpl(JTable aToModify, TLcdModelChangedEvent aModelChangedEvent){
ILcdModel model = aModelChangedEvent.getModel();
aToModify.setModel( createTableModel(model) );
}
}
The table can be garbage collected as soon as it is removed from the UI (so the dialog is closed), while the model still exists. There is no need to explicitly remove the listener, which makes it convenient to use.
Rules on using weak listeners:
- Use a weak listener when listening to an object that can exist longer than the object that needs to be updated.
- The listener must not keep a strong reference to the object to update (directly or indirectly).
- It is OK for the listener to keep a strong reference to the long living object (was not needed in example).
- Use a static inner class for the listener (or a regular class). The compiler can add extra fields to non-static or anonymous inner classes, so it is safer and simpler to use static inner classes.
- There is no need to remove the listener. When the object to modify gets garbage collected, the listener will remove itself (the moment the next event is received by the listener).
- Since:
- 2015.1
-
Constructor Summary
ModifierConstructorDescriptionprotected
ALcdWeakModelListener
(T aObjectToModify) Creates a newALcdWeakModelListener
instance which can updateaObjectToModify
when it receives events. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
modelChanged
(TLcdModelChangedEvent aEvent) Notifies this model listener that a model has changed.protected abstract void
modelChangedImpl
(T aToModify, TLcdModelChangedEvent aModelChangedEvent) This method is called each time aTLcdModelChangedEvent
is received by this listener, and the short-living object to modify passed in the constructor of this listener is not yet GC-ed.
-
Constructor Details
-
ALcdWeakModelListener
Creates a new
ALcdWeakModelListener
instance which can updateaObjectToModify
when it receives events.- Parameters:
aObjectToModify
- The object you want to modify when aTLcdModelChangedEvent
is received. This is a short-lived object, while the listener will be attached to a long-livingILcdModel
object.
-
-
Method Details
-
modelChanged
Notifies this model listener that a model has changed.See the
template code example
inTLcdModelChangedEvent
on how to handle model change events.This method will check whether the short-living object is still available. If it is, this method will pass the short-living object and the event to the
modelChangedImpl(Object, TLcdModelChangedEvent)
method. If the short-living object is already garbage collected, this method will de-register the listener from the source model.- Specified by:
modelChanged
in interfaceILcdModelListener
- Parameters:
aEvent
- theTLcdModelChangedEvent
that contains relevant information on the change(s).- See Also:
-
modelChangedImpl
This method is called each time a
TLcdModelChangedEvent
is received by this listener, and the short-living object to modify passed in the constructor of this listener is not yet GC-ed.- Parameters:
aToModify
- The object to modify, as passed in the constructor of this class.aModelChangedEvent
- The received event
-