在学习RMI的过程中,对于RMI package的设计思想产生了兴趣
我在javaeye上发了一个帖子,但是没有人理我,只能自己研究了
http://www.javaeye.com/post/142161
我在javaeye上发了一个帖子,但是没有人理我,只能自己研究了
http://www.javaeye.com/post/142161
Question:
java.rmi.Remote是一个interface,没有定义任何方法- 为何在开发RMI程序时必须要extends Remote接口呢?(这是一种设计模式:Marker interface,详见blog专题)
- 为何在实现类的时候要extends UnicastRemoteObject呢?(继承UnicastRemoteObject类是为了连接到RMI系统,但是继承这个类不是必须的,一个类如果不继承UnicastRemoteObject也可以使用UnicastRemoteObject的exportObject()方法来连接到RMI系统)
- RMI的底层通信部分应该也是利用Socket吧,是否是通过extends UnicastRemoteObject来实现的?
- 在RMI中使用的是怎样的一种design pattern?(多种design pattern,有marker interface、proxy pattern)
Answer:
the key to understanding RMI is to remember that interfaces define behavior and classes define implementation.在客户端程序中,只要定义接口就可以了;
服务器端负责具体的接口实现;
Remember that a Java interface does not contain executable code. RMI supports two classes that implement the same interface. The first class is the implementation of the behavior, and it runs on the server. The second class acts as a proxy for the remote service and it runs on the client.
A client program makes method calls on the proxy object, RMI sends the request to the remote JVM, and forwards it to the implementation. Any return values provided by the implementation are sent back to the proxy and then to the client's program.