Message Factory and Message Interface Design Pattern

Intent

Distributed systems involve several communicating processors. The interfaces between these processors are often governed by interface documents and standard header files. The main objective of the Message Interface Design Pattern is to decouple the software from its external interfaces. This pattern also allows for a smoother role out of interface by maintaining backward compatibility.

Also Known As

Motivation

Software developers of distributed system resist changes in interfaces between modules. This is mainly due to:

This pattern addresses these issues as mentioned below:.

Applicability

This pattern can be used in the following cases:

Structure

Participants

The key actors of this design pattern:

Collaboration

The following diagram shows the relationship and collaboration between various classes involved in the Message Factory and Message Interface Pattern.

Message factory and message interface design pattern UML class diagram

Consequences

Use of this pattern has the following benefits:

Implementation

The following scenarios are supported:

Setting Up Message Factory

  1. All message classes define a static method that creates an instance of the message by calling new.
  2. A function pointer to the above function, message type and version are passed to the Message Factory.
  3. Message Factory stores the mapping between message type, version and static message creation method.

Receiving a Message

  1. Application asks a Processor class to receive all messages from the OS queue.
  2. Processor obtains the message type and version from the message
  3. Message Factory is invoked with the message type and version
  4. Message Factory invokes the previously registered static method to create an object of the required type.
  5. The pointer to the message object is returned to the caller.
  6. Processor class now invokes Unpack for the message object and specifies the received message's pointer.
  7. Unpack method first unpacks the header of the message
  8. Once the header is unpacked, message specific field unpacking is done.
  9. After Unpacking is completed, the Processor specific message handler is invoked.

Sending a Message

  1. Application asks the Processor class to send a message object.
  2. The Processor class then invokes the Pack functionality to obtain a packed message.
  3. Packed message is passed to the operating system.

Sample Code and Usage

Message Interface Design Pattern (Framework)

Message Interface Design Pattern (Example)

Known Uses

Any type of message interfaces can be implemented using this pattern. A few examples are:

Related Patterns