liveBook · Manning

Mediator pattern

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

The Mediator pattern is a behavioral design pattern that centralizes communication among objects through a mediator to reduce their direct dependencies.

The Mediator pattern centralizes communication among objects in a mediator, allowing them to communicate indirectly instead of depending directly on one another. In a hexagonal architecture, it can separate a Kafka consumer from an AssetManager when direct adapter-to-domain coupling threatens the adapter’s responsibility. section 5.2 section 5.4

A practical implementation follows this flow:

  • Review generated code carefully: an AI assistant can suggest a useful decoupling strategy while still producing output that requires domain and quality checks. section 5.2 section 5.4

Decoupling asset-location updates

A Kafka consumer receives JSON containing an asset ID, latitude, longitude, and timestamp. It creates an AssetLocationUpdated event and publishes it through AssetLocationMediator. AssetManager subscribes to that event, reads the asset, adds the new location, and remains unaware of Kafka’s consumer implementation.

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Explains the pattern through a mediator implementation, constructor wiring, event publication, and handler registration.
Examples
Uses an AssetLocationMediator, AssetLocationUpdated events, AssetManager handlers, and Kafka asset_location messages.
What it assumes
Assumes familiarity with Python classes, Kafka consumers, domain models, and hexagonal architecture.
What you can do after
Build it from scratch by introducing a mediator, injecting it into a consumer, and registering domain handlers for published events.
How it connects
Connects behavioral design patterns with loose coupling, hexagonal architecture, ports and adapters, event-driven communication, and Apache Kafka.

Also explained in

Compare how 2 books explain mediator pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.