// get a class responsible for implementing IConfig
IConfig cfg;
// reading a simple key
cfg.getValue("/section1/key1") -> value1
// reading a key with reserved name attribute
cfg.getValue("/section2/key/x") -> value_for_x
// reading a key with reserved name attribute and its
// attribute/child nodes
cfg.getValue("/section2/key/y/a1") -> 10
cfg.getValue("/section2/key/y/a3") -> 16
//Let us define an interface
interface LoggingInterface
{
public void logMessage(String message);
}
//Let's see how we can use this interface
public void myLoggingFunction( Object o)
{
if (o is LoggingInterface)
{
LoggingInterface log = (LoggingInterface)o;
log.message("test message");
}
else
{
// sorry not sure of the type of this object
throw new Exception("Unexpected type");
}
}
public class FileLogger : LoggingInterface
{
private FileOutputStream fs = null;
public FileLogger(string filename)
{
fs = new FileOutputstream(filename);
}
// implement the following method to complete the
// contract required by LoggingInterface
public void logMessage(String message)
{
fs.println(message);
}
public close()
{
fs.close();
}
}
好了,您现在可以加入下列代码:
FileLogger fileLogger = new FileLogger("myfile");
myLoggingFunction(fileLogger);
public LoggingInterface getLogger()
{
IConfig cfg = ...
string loggerClassname = cfg.getValue
("/request/logger/classname","EventLogger");
Object o = instantiateAnObjectForItsClassname(
loggerClassName);
// code is not provided for this function
LoggingInterface log = (LoggingInterface)o;
return log;
}
public static Object getObject(string interfacename)
{
IConfig cfg = ...
string interfaceClassname = cfg.getValue("/request/" +
interfacename + "/classname");
Object o = instantiateAnObjectForItsClassname(
loggerClassName);
// code is not provided for this function
return o;
}