Design Pattern: Master-Slave

 

Intention

 

Introduction

 

Example

 

Context

 

Problem

 

Solution

 

Applicability

 

Structure

Class
Master

Responsibility

  • Partitions work among serveral Slave components
  • Starts the execution of Slaves
  • Computes a result from the sub-results the Slaves return

Collaborators
Slave

 

Class
Slave

Responsibility

Implements the sub-service used by the Master

Collaborators
-

 

General Scenario

Client call Master

Master internally split work, callSlaves

Slaves are called and sub-result returns

Master internally combine results

Result return to Client

 

Implementation

Five simple steps

Can be implemented as passing parameters sharing or using separated data sturcture

Can be implemented as folking off Slave with the Master environment variables

Depends on initial problem

Classic example in database processing with data warehouse type of query

Build logical query plans - then pick the best one (using separate data structure)

After picking the best one, distributed out the work to Slave again with different interface for processing the picked plan

Split work into fix number of sub-tasks

Define as many sub-tasks as necessary

Need to handle when some Slave failed

Need to pipeline if necessary (e.g. IO will block, where computation usually goes faster, etc)

 

Variants

 

Consequences

Advantages

Disadvantages