Collector State Pattern

Realtime software sometimes involves collecting a sequence of messages. Further action can be initiated only after complete sequence of messages has been received. Collector state pattern handles such scenarios.


The main intention here is to manage sequence of events of same type. For example, a Call object needs to collect all the digits to perform call routing. This will involve activities like a timer start before each digit, digit collection, timer stop at each digit collection, timeout on complete digit collection and call routing on complete digit collection.

Also Known As


The main objective of this design pattern is to efficiently handle scenarios where message collection is involved. This involves counting the received messages and recognizing the point where collection has been completed.


The Collector State pattern can be used wherever there is a need to collect a sequence of messages. It may also be used in handling of periodic events like sanity punching.


This pattern consists of the following event handlers:


The key participants in this pattern are the task sending the sequence of small messages and the task collecting the message sequence to assemble a bigger message or meaningful data.


The state machine implementing the collector state patterns collaborates with the source of messages being collected and in some scenarios might report completion of collection to the originator.


Collector state pattern can result in triggering a state transition corresponding to successful collection of messages. If a timeout takes place before the collection has been completed, a state transition to an error handling state might be initiated.


As mentioned earlier, the components constituting Collector State pattern are Collected Message handler and Timeout handler. The Collector State pattern performs a timer start for each new small message collection, timer stop at each small message collection and state exit on timeout to Call Routing state or Partial Dialing state.

Here is an example of implementation of digit collection using this pattern.

  1. Start timer of inter-digit timeout size.
  2. Call digit collection method on receipt of digit message.
  3. Restart timer for next digit.
  4. The digit collection can end in any of the following ways.
    • On timeout, call 'call routing' function and perform state exit.
    • On receipt of "end of digits" condition in digit message, transition to "call routing" state.
    • On receipt of given number of digit messages, transition to "call routing" state.

Sample Code and Usage

Here is the sample code for a basic implementation of digit collection using the Collector State pattern.

Digit Collection Using the Collector State Pattern

Known Uses

This design pattern has been used in the following cases:

Related Patterns

Feature Coordination Design Patterns like parallel and serial coordination are examples of other more general mechanisms of handling a sequence of messages.