Home : Object Orientation : Router Pattern

Router

Intent

Allow object to subscribe and unsubscribe from messages in an event based system.

Motivation

Windows in a GUI (graphical user interface) client program are opened and closed throughout the lifetime of the program. The server should not be aware of which windows are open, or which are interested in specific data changes.

A solution is to provide a single bridge that routes events to clients/windows at their request.

Applicability

Use the router pattern when:

Structure

Class diagram

Participants

Collaborations

Sequence diagram

A producer places a message on the queue.

An arbitrary time later, the router gets the message from the queue and checks its map to find if a Route exists the for message. If a Route object is found, the Message object is passed to it for processing.

The Route object loops through all its Consumer objects passing the message to each in turn.

After the message has been processed, the Router object discards it

Consequences

The router pattern has the following consequences:

Implementation

Consider the following when implementing the Router pattern:

Sample Code

The following code snippets make use of the STL templates:

Related Patterns

Smalltalk's Model/View/Controller pattern and MFC's Document/View paradigm provide similar functionality but in a more restrictive, GUI centric manner.

Comments

Members have left 0 comments about this page:
Please Login or Register to comment on this page.

Resources
Tools
User
Last Updated Wednesday, 24-May-2006 22:45:03 BST