Package jmri

Class InstanceManager

java.lang.Object
jmri.InstanceManager

public final class InstanceManager
extends Object
Provides methods for locating various interface implementations. These form the base for locating JMRI objects, including the key managers.

The structural goal is to have the jmri package not depend on lower packages, with the implementations still available at run-time through the InstanceManager.

To retrieve the default object of a specific type, do getDefault(java.lang.Class<T>) where the argument is e.g. "SensorManager.class". In other words, you ask for the default object of a particular type. Note that this call is intended to be used in the usual case of requiring the object to function; it will log a message if there isn't such an object. If that's routine, then use the getNullableDefault(java.lang.Class<T>) method instead.

Multiple items can be held, and are retrieved as a list with getList(java.lang.Class<T>).

If a specific item is needed, e.g. one that has been constructed via a complex process during startup, it should be installed with store(T, java.lang.Class<T>).

If it is desirable for the InstanceManager to create an object on first request, have that object's class implement the InstanceManagerAutoDefault flag interface. The InstanceManager will then construct a default object via the no-argument constructor when one is first requested.

For initialization of more complex default objects, see the InstanceInitializer mechanism and its default implementation in DefaultInstanceInitializer.

Implement the InstanceManagerAutoInitialize interface when default objects need to be initialized after the default instance has been constructed and registered with the InstanceManager. This will allow references to the default instance during initialization to work as expected.


This file is part of JMRI.

JMRI is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. See the "COPYING" file for a copy of this license.

JMRI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.