![]() | Documentation Contents |
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.
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.
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
- The
FactoryImplregisters, or is registered, with thermiregistry- The client requests a reference to a
Factory- The
rmiregistryreturns a remote reference to aFactoryImpl- The client invokes a remote method on the
FactoryImplto obtain a remote reference to aProductImpl- The
FactoryImplreturns a remote reference to an existingProductImplor to one that it just created, based on the client request- The client invokes a remote method on the
ProductImpl
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.The Bank
In code,
AccountManagerwould be a remote interface with one or more remote methods. These methods would return objects that implement theAccountinterface. In a similar fashion,Accountwould 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
AccountManagerimplementation would be registered with the Java RMI registry. TheAccountManagerimplementation would be the factory, returning remote references to (or serialized instances of)Accountimplementations, like your savings account.The Library
In the library example, the
Librarianwould be a remote interface with one or more methods that would return objects that implement theLibraryCardinterface. In addition, theLibrarianinterface would have methods to allow you access to books, CDs, or videotapes that implemented theLoanableinterface.In Java RMI, only the instance of the
Librarianimplementation would be registered with the Java RMI registry. TheLibrarianimplementation would be the factory, returning remote references to (or serialized instances of)LibraryCardimplementations andLoanableobject implementations.
Copyright © 1993, 2010, Oracle and/or its affiliates. All rights reserved. Please send comments using this Feedback page. |
Java Technology |