# Applying the Factory Pattern to JavaTM 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.By using a factory in JavaTM Remote Method Invocation (Java RMI), you can reduce the number of objects that you need to register with the Java RMI registry.

## Examples of Factories in the Real World:

> ### The Bank

> When you go to the bank to make a deposit to your account, you don't walk up to a vault, pull out a drawer with your name on it, drop in your money, shut the drawer and leave. Think about how you originally established, or opened, the account. You probably went to the bank, spoke with an Account Manager, and signed some papers. In return, they gave you some checks, a passbook, or a bank card so you could access your account in the future.

> The Account Manager is an example of a factory. The person or Automated Teller Machine (ATM) that acts as account manager controls the creation of and/or access to individual accounts.

> ### The Library

> Let's think about how a book, compact disk, or video tape gets from the library shelf into your home. Before you can check out any material, you must first get a library card from the librarian. In this case, the librarian could be viewed as a library card factory because the librarian controls the creation of new library card instances.

> Once you have a library card, you can go into the library, and without any further fuss, just walk out with all your materials, right? Of course not. Before you can walk out of the library without setting off the alarm system, you must check out the book, CD, or video tape you wish to take home. So you present your library card to, you guessed it, the librarian, who will use your card to access the library database to see if you owe any late fees, and to register these new materials as having been leased to you. In this case, the librarian could be seen as a book factory because the librarian controls your access to the books.

## How Does a Factory Work in Java RMI?

Just like any other Java RMI program, there are 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 initial contact with the server.

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

- There are two remote interfaces that the client understands, `Factory` and `Product`
- The `FactoryImpl` implements the `Factory` interface and the `ProductImpl` implements the `Product` interface

> 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`

## How Could the Bank and Library be Implemented in Java RMI?

> ### The Bank

> In code, `AccountManager` would be a remote interface with one or more remote methods. These methods would return objects that implement the `Account` interface. In a similar fashion, `Account` would be an interface that declared all of the operations a person could perform on an account instance, such as depositing or withdrawing money, getting an account balance, or listing the most recent account transactions.

> In Java RMI, only the instance of the `AccountManager` implementation would be registered with the Java RMI registry. The `AccountManager` implementation would be the factory, returning 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, CDs, or videotapes that implemented the `Loanable` interface.

> In Java RMI, only the instance of the `Librarian` implementation would be registered with the Java RMI registry. The `Librarian` implementation would be the factory, returning 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 Java RMI.

Copyright © 2006 Sun Microsystems, Inc. All Rights Reserved. Please send comments to: rmi-comments@java.sun.com | Java Software