Java Reference
In-Depth Information
After the registry has been created, we continue as in Chapter 18 by instantiating
a
ServerFactory
object and binding it into the registry under the name in the
FACTORY
-
NAME
constant:
try {
ServerFactory factory = new ServerFactory ();
Naming.rebind ("//localhost:" + port + "/" + FACTORY
-
NAME,
factory);
System.out.println(
"
\
n READY AND WAITING ON CLIENTS ON PORT " + port);
}
As usual we must catch the following exceptions:
catch (MalformedURLException mue) {
System.err.println (mue);
System.exit (1);
}
catch (RemoteException re) {
System.err.println (re);
System.err.println ("
\
n EXITING BECAUSE OF FAILURE");
System.exit (1);
}
20.4.2 Implementing
getInstance()
Our
getInstance()
implementation receives the ID parameter and returns an
object that implements
ServerInterface
:
public ServerInterface getInstance (String id)
throws RemoteException
{
...
}
For this example, we simply create a
Server
object for return. We haven't yet
discussed the
Server
implementation, and although we know the signatures of
the methods it must contain (since it must implement
ServerInterface
), we
don't yet know how to call its constructor. Often, implementations evolve through
an iterative process, so let's start by calling the constructor as follows:
ServerInterface server = new Server (id);
Our
getInstance()
needs to return a
ServerInterface
, not a
Server
object, so we declare the object returned by the
Server
constructor to be of type
ServerInterface
. (It is, in fact, a
Server
object, but since it implements
ServerInterface
,itisalso of type
ServerInterface
.)
As shown, we also passed the
id
value received by
getInstance()
to
the
Server
constructor. From the discussion above, the ID parameter serves to
Search WWH ::
Custom Search