👉 A code and a full explanation are here: https://github.com/fadeevab/mediator-pattern-rust (a Train Station example).
Rust rejects a typical approach to implementing some Design Patterns. It's so tricky so I decided to put the whole pattern into a separate repo.
👆 I managed to implement a minimal Mediator pattern example in 2 ways.
- A "naive" approach, a reference-counting object model, mimicking a typical OOP language,
- top-down ownership approach.
The key point is thinking in terms of OWNERSHIP.
- A mediator takes ownership of all components.
- A component doesn't preserve a reference to a mediator. Instead, it gets the reference via a method call.
- Control flow starts from the fn main() where the mediator receives external events/commands.
- Mediator trait for the interaction between components is not the same as its external API for receiving external events (commands from the main loop).
What is not covered, at least yet: asynchronous approaches, queue-based event handling with bubble-up capability.