WebServer server = new WebServer(8585);
// Create a handler class
HelloHandler hello = new HelloHandler( );
server.addHandler("hello", hello);
The server class does not distinguish between these methodologies, as
long as the handler class is instantiated when it gets passed into
the addHandler( ) method. So you can make a small
change to this code if you want to add an instance of the singleton
Scheduler class described previously:
WebServer server = new WebServer(8585);
// Pass in the singleton instance
server.addHandler("scheduler", Scheduler.getInstance( ));
This passes in the shared instance just as if the class were being
instantiated through a constructor with the new keyword, and
preserves any information shared across the singleton class. Many
classes used in services such as XML-RPC are built as singletons to
avoid the use of static data variables, as a shared instance allows
the data to be stored in member variables; the single instance then
operates upon those member variables for all client requests.