JAVA案例开发集锦源代码 袁然 郑自国编
源代码在线查看: playlistfactory.java
package javazoom.jlGui.playlist;
import javazoom.Util.*;
import java.lang.reflect.*;
//PlaylistFactory.
public class PlaylistFactory
{
private static PlaylistFactory _instance = null;
private Playlist _playlistInstance = null;
private Config _config = null;
/**
* Constructor.
*/
private PlaylistFactory()
{
_config = Config.getInstance();
}
/**
* Returns instance of PlaylistFactory.
*/
public synchronized static PlaylistFactory getInstance()
{
if (_instance == null)
{
_instance = new PlaylistFactory();
}
return _instance;
}
/**
* Returns Playlist instantied from full qualified class name.
*/
public Playlist getPlaylist()
{
if (_playlistInstance == null)
{
String classname = _config.getPlaylistClassName();
boolean interfaceFound = false;
try
{
Class aClass = Class.forName(classname);
Class superClass = aClass;
// Looking for Playlist interface implementation.
while (superClass != null)
{
Class[] interfaces = superClass.getInterfaces();
for (int i = 0; i < interfaces.length;i++)
{
if ( (interfaces[i].getName()).equals("javazoom.jlGui.playlist.Playlist") )
{
interfaceFound = true;
break;
}
}
if (interfaceFound == true) break;
superClass = superClass.getSuperclass();
}
if (interfaceFound == false)
{
trace(0,""+getClass().getName(),"Error : Playlist implementation not found in "+classname+" hierarchy");
}
else
{
Class[] argsClass = new Class[] {};
Constructor c = aClass.getConstructor(argsClass);
_playlistInstance = (Playlist) (c.newInstance(null));
trace(0,""+getClass().getName(),classname + " loaded");
}
} catch (Exception e)
{
trace(0,""+getClass().getName(),"Error : "+classname+" : "+e.getMessage());
}
}
return _playlistInstance;
}
/**
* Sends traces to Debug.
*/
private void trace(int level, String msg1, String msg2)
{
Debug dbg = Debug.getInstance();
dbg.log(level,msg1+":"+msg2);
}
}