Java Reference
In-Depth Information
One might recall from Chapters 14 and 15 that we handled the multiple client
problem by using multiple threads on the server - one thread per client. That
solution is, in fact, very close to the more formal design pattern technique pre-
sented here. The relationship between the obvious multithreaded solution used
earlier and the design pattern used below will become obvious as we proceed.
The need to handle multiple clients is manifestly a common design problem,
and a useful design pattern for this common situation is the Factory Pattern ,
which specifies that multiple server objects are obtained from a server factory .A
factory is simply an object that creates other objects. Therefore, when we move
from conceptual model diagrams to object diagrams, the client object connects
first not to a server but to a server factory object. The server factory knows
how to create server objects. From the factory object, the client obtains a new
server object. Further interaction then occurs between the client and its own pri-
vate server. Other clients, typically on other machines, obtain additional server
objects from the factory. Thus the Factory Pattern is a technique to support
multiple simultaneous clients. This pattern is very common in distributed pro-
gramming, and its relationship to the thread-per-client solution used previous is
obvious.
It is illuminating to consider attempting to implement the Factory Pattern in
some language other than Java. It might be straightforward to have the factory
create new server objects as needed, but if one server object has control of the
CPU, then the other servers will never get a chance to execute. A necessary
condition for properly implementing the Factory Pattern is the ability for each
server object to get a portion of the CPU. One particularly heavyweight solution
is to create a new process on the server machine for each new server object, letting
the operating system take care of CPU allocation among the processes. Often a
higher performance solution is to use multiple threads of execution. Since Java
has an easy-to-use multithreading API, each new server object can run in its own
separate thread (see Chapter 8). If a limit on the number of simultaneous clients
is required, for example to keep from overloading the server machine, then the
factory can easily keep track of the number of server objects in use and throw a
“too busy” exception if that limit is exceeded.
16.5 Collaboration diagram for a simple
distributed application
A collaboration diagram illustrating the factory pattern is shown in Figure 16.3.
Here the arrows indicate “messages” from one object to another. The arrowhead
points to the object that receives the message - i.e. to the object that provides
the method. The sequence numbers indicate the order of the operations and may
be nested. Thus, in sequence number 1, the client sends a getInstance()
message to the ServerFactory object. That is, the client, which already has
Search WWH ::




Custom Search