Manager Design Pattern

Realtime software generally manages multiple entities of the same type. Here we introduce the Manager Design Pattern to control multiple entities. The Manager design pattern is described using a standard pattern definition template.

Intent

The main intention here is to manage multiple entities of same type. For example, a Terminal Manager will manage all the Terminal objects under its control. This will involve Terminal creation, Terminal deletion, message routing to the Terminal and coordination of activities like switchover between two Terminals.

Also Known As

  • Manager-Managed Design Pattern
  • Managed Object Design Pattern

Motivation

Consider the case where no Manager object is defined. Each Terminal object has to know about all other Terminal objects for purposes of message routing and coordination. Also, in the absence of a Manager there will be no single point for Terminal object creation and deletion.

Applicability

This design pattern can be applied whenever a system needs to support many entities of same or similar type. The Manager object is designed to keep track of all the entities. In many cases, the Manager will also route messages to individual entities.

Structure

The Manager object may manage all the entity objects by implementing standard data structures like array or STL map. The idea is to have a quick way of indexing to get to a particular entity. This indexing capability is required to quickly access an entity from a numerical id assigned to the entity. This numerical id will be typically included in all the messages received for these entities.

Participants

The Manager object and the Managed Entities are the main actors in this design pattern. A single Manager object manages multiple entity objects.

Collaboration

Manager to Managed Entity communication is typically done via manager invoking methods for the individual entities. The return value of a method is used by the entity to communicate with the manager. The routing of messages is also achieved by invoking the message handler for the entity object.

In more complicated designs, the Manager and entity may communicate via local messages. In such cases, the various coordination design patterns like parallel and serial coordination may be used.

Consequences

The Manager object introduces a clean interface for communicating with any of the managed entities. It also provides a centralized point for coordinating operations on multiple entity objects.

Implementation

As mentioned earlier, the Manager object contains an array or STL map of all the entities under its control. The entity collection may be maintained as pointers to the entity objects or entity objects themselves.

Sample Code and Usage

The code below gives an example of a Terminal Manager which manages Terminal objects. The example covers the message routing, creation and deletion of terminals. An interaction involving multiple terminals is also shown (Terminal Switchover). Also note that the terminal object methods communicate with the Terminal Manager by returning a status value.

Terminal Manager and Terminal

Known Uses

The manager design pattern is used wherever there is a need to manage multiple objects of same or similar behavior.

Related Patterns

Feature Design Patterns like parallel and serial coordination