Patterns: behavior · 3 MIN READ

Mediator: centralize collaboration deliberately

Let participants communicate through a focused coordinator.

Let participants communicate through a focused coordinator. Mediator moves interaction rules out of a set of objects that otherwise know one another directly. Trace the worked Go example, then compare the benefit with the cost before applying this design to your own project.

Behavioral pattern · A MAP OF THE IDEA

  1. Participants expose focused operations
  2. Mediator decides the interaction order
  3. Participants stay independent of one another

Useful when

  • Many peers otherwise reference one another
  • A workflow needs one place for coordination rules

Watch out

  • An oversized mediator becomes a god object
  • Two simple peers may not need coordination machinery

Implementation & operation cost: Medium

Fit depends on the task

Small tool or CLI2/5
Domain-rich application4/5
Independent distributed services3/5

Editorial suitability, not benchmarks or a universal ranking. Styles describe different axes and can be combined.

01

The design decision

Mediator moves interaction rules out of a set of objects that otherwise know one another directly. A checkout coordinator can connect validation and notification without making each participant depend on every other. The mediator should represent a focused workflow rather than become the whole application.

02

A complete Go example

Read this program from the entry point and follow the calls shown in the diagram. The output is included at the end so you can check the behavior without a compiler.

go
package main

import (
	"fmt"
)

type Mediator struct {
	validate func(string) bool
	notify   func(string)
}

func (m Mediator) Submit(name string) bool {
	if !m.validate(name) {
		return false
	}
	m.notify(name)
	return true
}

func main() {
	sent := ""
	m := Mediator{validate: func(s string) bool { return s != "" }, notify: func(s string) { sent = s }}
	fmt.Println(m.Submit("Ada"), sent)
	fmt.Println(m.Submit(""))
}

// Output:
// true Ada
// false
Read the code

Submit owns the collaboration rule: notification only follows valid input. The two collaborators do not reference each other. This uses function dependencies because the roles are small and their lifetime is controlled by main.

03

Tradeoffs and the simpler alternative

As participants grow, split mediators by use case instead of accumulating unrelated branches. A facade mainly simplifies client access; a mediator coordinates participant interaction. Direct calls are easier when only two simple components collaborate once.

TAKE THIS WITH YOU

Centralizes interaction rules among collaborators. As participants grow, split mediators by use case instead of accumulating unrelated branches. A facade mainly simplifies client access; a mediator coordinates participant interaction. Direct calls are easier when only two simple components collaborate once.

Check your understanding

Which statement best describes Mediator: centralize collaboration deliberately?

Go to the source

Progress stays in this browser. No account needed.