Java Reference
In-Depth Information
Because of the amount of information that is passed, the throughput of information should be done as fast as
possible, or the system slows down dramatically. If the throughput fails or is disruptive for some reason, that
specific target is dropped. By implementing this kind of control, the quality of service can be guaranteed for the
other destinations.
Implementation
Figure 4.10. Router class diagram
The Router pattern needs the following:
InputChannel -
Can be of any type. It is used for mapping
Input
and
Output
in the
Router
; also known as the
source.
OutputChannel -
An interface that defines the method for sending the message.
Message -
This class contains the information that needed to be distributed. To allow the
Router
to map this
message to a specific route, the message also contains a reference to the source.
Router -
The
Router
maintains the map between
InputChannel
and specific
OutputChannels
, and
implements the
OutputChannel
interface. When it receives messages, it forwards them to the specified
OutputChannels
.
Benefits and Drawbacks
Benefits and drawbacks include the following:
Decouples the source from the destination. The input doesn't need to know the destination; it only needs to know
the router. The router knows the mapping between the source and destination.
When trouble occurs on one specific channel, it doesn't need to affect the other channels. One channel does not
have the ability to block the
Router
, it continues doing its work of routing messages from an input to outputs.
Different strategies for
InputChannels
and
OutputChannels
. The Router can have each
Channel
in its own
Thread
or combine all the
InputChannels
in one
Thread
.
Simplifies the
Clients
, because the
Router
takes over the task of message distribution.
Enhances reliability. One channel can no longer block the system. If it's not working, the
Channel
can be ignored or other
measures can be taken like dropping the problem channel. However, the
Router
still does its job.
Pattern Variants
A variation to this pattern is to have the
Router
keep a mapping between an arbitrary key and the destinations. A
source might have different destinations based on certain conditions. For instance, if it has two separate methods
each with its own
OutputChannels
. In the normal implementation of the Router pattern there is only one route