Applying the Factory Pattern to RMI

What is a "factory" and why would you want to use one? A factory, in this context, is a piece of software that implements one of the "factory" design patterns introduced in the book, Design Patterns, Elements of Reusable Object-Oriented Software.  In general, a factory implementation is useful when you need one object to control the creation of and/or access to other objects. Specifically, in RMI, by using a factory, you can reduce the number of objects that you need to register with the RMI registry.

Examples of Factories in the Real World:

How Does a Factory Work in RMI?

Just like any other RMI program, you have a few basic players: a server that produces one or more remote objects, each of which implements a remote interface, a client that accesses a name server (the rmiregistry) to get a reference to one of the remote objects, and the rmiregistry which facilitates the client's ability to initially contact the server.

For the picture below, and the steps that follow, you may make the following assumptions:

       
    1. The FactoryImpl registers, or is registered, with the rmiregistry
    2. The client requests a reference to a Factory
    3. The rmiregistry returns a remote reference to a FactoryImpl
    4. The client invokes a remote method on the FactoryImpl to obtain a remote reference to a ProductImpl
    5. The FactoryImpl returns a remote reference to an existing ProductImpl or to one that it just created based on the client request
    6. The client invokes a remote method on the ProductImpl

    7.  

How Could the Bank and Library be Implemented in RMI?

    The Bank

    In code, AccountManager would be a remote interface with one or more methods that would return objects that implement the Account interface. In a similar fashion, Account would be an interface that declared all the operations a person could perform on an account instance, like deposit or withdraw money, get an account balance, or get the most recent account transactions.

    In RMI, only the instance of the AccountManager implementation would be registered with the RMI registry. The AccountManager implementation would be the factory that returned remote references to (or serialized instances of) Account implementations, like your savings account.

    The Library

    In the library example, the Librarian would be a remote interface with one or more methods that would return objects that implement the LibraryCard interface. In addition, the Librarian interface would have methods to allow you access to books or CD's or videotapes that implemented the Loanable interface.

    In RMI, only the instance of the Librarian implementation would be registered with the RMI registry. The Librarian implementation would be the factory that returned remote references to (or serialized instances of) LibraryCard implementations and Loanable object implementations.

While the bank and library examples presented here, may not be entirely complete they are not designed to be complete, but rather instructionally useful in describing the factory pattern in RMI.


Copyright © 1998 Sun Microsystems, Inc. All rights reserved.