Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Design networked applications in RMI using the Adapter design pattern

A guide to correctly adapting local Java objects for the Web

  • Print
  • Feedback
If you use Java's Remote Method Invocation (RMI), you know that it's easy to understand and implement: The perfect recipe for creating remote objects and services. In fact, Sun's Java tutorial provides an excellent example for creating your first RMI server and client and for running a remote system. However, I've seen many designs that build upon the Sun example and become unwieldy as new functions are added. Novice programmers often resort to odd naming conventions to distinguish local behavior from remote behavior, and, with the addition of new remote services, the remote class can easily grow into a big mess that performs poorly under remote network traffic.

In this article I'll demonstrate how the Adapter pattern, one of the most common design patterns, can be used to adapt a local class to a remote class. Using this design technique, it's easy to distinguish local from remote behavior, and you'll be able to continue to use the local class for your non-networked applications. Also, as the number of remote services grow, it's easy to see which class must handle the request and which objects must be synchronized. Finally, with this technique, your design will be easier to understand and more amenable to changes and fixes from multiple developers.

What is the Adapter pattern?

The Adapter pattern is one of the structural patterns listed in the reference book Design Patterns by the Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides). The Adapter pattern is very common not only to remote client/server programming but also to any situation in which you have one class that you wish to reuse, but the application interface doesn't match the class interface. For this reason, you use an adapter or wrapper to convert the application interface to the existing class interface. In software development, an adapter simply maps the interface of one class to that of another. Adapters are used continually throughout the software development process, hence the term Adapter pattern.

Figure 1, below, illustrates how an adapter works. In this diagram, Client wants to invoke the method request() in the Target interface. Since the Adaptee class has no request() method, it is the job of Adapter to convert the request to an available matching method. Here the Adapter converts the method request() call into the Adaptee method specificRequest() call. The Adapter does this conversion for each method that needs adapting, also known as wrappering.

Figure 1. Adapting an interface to an Adaptee class



This type of adapting is exactly what one needs when creating remote services for the Web. Think of the design process as taking a working local class and publishing its services for the Web. You publish the services not by adding to the local class, but by adapting the local class to the remote interface. In the following sections I'll show you how this is done.

Designing a networked application

As stated in the previous section, classes are best made Web-ready by adapting the local methods to a remote interface. Sun's Java tutorial tells us we have to follow certain steps to make RMI work. These steps are as follows:

  • Print
  • Feedback

Resources
  • Sun's Java tutorial on RMI -- an excellent starting place for RMI beginners http://java.sun.com/docs/books/tutorial/rmi/TOC.html
  • Sun's page on the Java Collections interfaces in Java 2 http://java.sun.com/products/jdk/1.2/docs/guide/collections/index.html
  • Design PatternsElements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison Wesley, 1994) A solid overview of design patterns http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201633612
  • Patterns in Java, Volume 1, Mark Grand (John Wiley & Sons, 1998) A good resource for learning about patterns with specific implementations in Java http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0471258393