Java Reference
In-Depth Information
Router
Also known as Request Router, Multiplexer
Pattern Properties
Type: Concurrency
Level: Architectural
Purpose
To decouple multiple sources of information from the targets of that information.
Introduction
As a socially and professionally sought-after PIM user who exchanges information frequently with other PIM
users, you put a significant strain on the application. There is, as a result, a lot going on in the PIM application,
and many parts of the same application might need to respond to or be informed of the same event. This is not
necessarily a GUI event, but might be just anything that is going on. Your boss might want to know if your 2:00
to 3:00 appointment clears up so you can go to another meeting in her place. You might want to know if a certain
stock price plunges, so you can buy another 100 shares cheap.
In a complex system like the PIM, there can certainly be multiple sources of information. There can also be
multiple destinations for information—multiple parties who want to receive information from the same event
source. To make things easier on an event source and to save it the effort of notifying all listeners, it's a good idea
to separate the distribution of the event into a distinct entity. The Router pattern follows this practice, since it
combines multiple event source and multiple listeners.
Without a way to identify individual clients, the server will be limited to operations that could be performed in a
single operation. This would force you to make one of two choices. You could develop a huge operation—we're
talking an operation that would show up on satellite photos—that would hold all data. You could redesign the
application model so it could handle stateless operations, perhaps designing it around a checkout system for the
data. Of course, that would mean that you would also have to plan for error recovery, and security for the
check-out system. When you get right down to it, neither choice is very appealing. Each solution would require a
radical redesign of the application and the way the user interacts with the system.
Applicability
Use Router when:
There are multiple sources of information
There are multiple destinations receiving that information
Description
In the PIM, information is central and is shared between the many parts of the application and the different
applications. The distribution of the information could be handled by each source.
Then every destination has to be registered with the source and that source has to maintain a list of all destinations
and notify them when a change has occurred. Suppose you wanted to enhance your application by using
multithreading when sending messages; you would have to implement that for each source. It would be much
easier to manage the distribution of information separately, which is what the Router pattern does.
The Router pattern works like a router in network traffic. It receives information from a source and determines,
based on where the information is coming from, to which destination(s) it should send the message. To be able to
do that the router keeps a mapping between the various input channels (sources) and their destinations. The
management of the each of the routes from a source to its destinations is captured in the mapping. Destinations
can be added and removed by calling the appropriate methods on the router instead of on the source.
Search WWH ::




Custom Search