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
ConstructorsModifierConstructorDescriptionprotectedALcdWeakModelListener(T aObjectToModify) Creates a newALcdWeakModelListenerinstance which can updateaObjectToModifywhen it receives events. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidmodelChanged(TLcdModelChangedEvent aEvent) Notifies this model listener that a model has changed.protected abstract voidmodelChangedImpl(T aToModify, TLcdModelChangedEvent aModelChangedEvent) This method is called each time aTLcdModelChangedEventis 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
ALcdWeakModelListenerinstance which can updateaObjectToModifywhen it receives events.- Parameters:
aObjectToModify- The object you want to modify when aTLcdModelChangedEventis received. This is a short-lived object, while the listener will be attached to a long-livingILcdModelobject.
-
-
Method Details
-
modelChanged
Notifies this model listener that a model has changed.See the
template code exampleinTLcdModelChangedEventon 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:
modelChangedin interfaceILcdModelListener- Parameters:
aEvent- theTLcdModelChangedEventthat contains relevant information on the change(s).- See Also:
-
modelChangedImpl
This method is called each time a
TLcdModelChangedEventis 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
-