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. 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 Java Remote Method Invocation (Java RMI), you can reduce the number of objects that you need to register with the Java RMI registry.
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.
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.
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:
Factory and ProductFactoryImpl implements the
Factory interface and the ProductImpl
implements the Product interface
FactoryImpl registers, or is registered, with
the rmiregistryFactoryrmiregistry returns a remote reference to a
FactoryImplFactoryImpl to obtain a remote reference to a
ProductImplFactoryImpl returns a remote reference to an
existing ProductImpl or to one that it just created,
based on the client requestProductImplIn 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.
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.