• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI Interface

 
Tim Adam Cooper
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning about RMI for the Development project. I have written the following programs:
//Interface
import java.rmi.*;
public interface WeatherIntf extends Remote {
public String getWeather() throws RemoteException;
}
//Dummy Class
class Dummy{
public String getWeather(){
return Math.random()>0.5? "sunny" : "rainy";
}
}
//Server Class
import java.rmi.server.*;
import java.rmi.*;
public class Server extends UnicastRemoteObject
implements WeatherIntf {
private Dummy d;
public Server () throws RemoteException {
super();
d = new Dummy();
}
public String getWeather() throws RemoteException {
return d.getWeather();
}
public static void main(String[] args) {
try {
Server myWeatherServer = new Server();
Naming.rebind("//timothycooper/TestServer",myWeatherServer);
System.out.println("Server Ready");
}catch (Exception e) {
System.out.println(e.getMessage() );
}
}
}
//Client Class
import java.rmi.*;
public class Client {
public static void main(String[] args) {
try {
Remote robj = Naming.lookup("//timothycooper/TestServer");
WeatherIntf weatherserver = (WeatherIntf) robj;
String forecast = weatherserver.getWeather();
System.out.println("The weather will be " + forecast );
} catch (Exception e) {System.out.println(e.getMessage());}
}
}
It works with no problem on the network.
Question:
I think that some people will say that the Dummy instance found in the Server class’s constructor probably will not be remoteable and that the Dummy class should implements the WeatherIntf interface.
I ask this question because there seem to be a common approach that in the Java Development FlightNight project that the class Data should implements DataInterface but it look as if the Data class need not implements the DataInterface?
Can anybody shed some light on it?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Data class does not need to implement the DataInterface if you chose not to. In my submission I did not have the Data class implement that interface.
Now by implementing that interface, you can now use the Data class in any class or method that expects a class of DataInterface type. Whereas without implementing the interface, that would not be acceptible.
Pros and Cons are up to you, and others can give you some if they want.
The choice is yours. I prefer if it implemented it, just as a nice design.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic