DEV Community

Sergey Leschev
Sergey Leschev

Posted on • Updated on

💐 Mediator Pattern

Mediator Pattern

The mediator pattern is used to reduce coupling between classes that communicate with each other. Instead of classes communicating directly, and thus requiring knowledge of their implementation, the classes send messages via a mediator object.



protocol Receiver {
    associatedtype MessageType
    func receive(message: MessageType)

protocol Sender {
    associatedtype MessageType
    associatedtype ReceiverType: Receiver

    var recipients: [ReceiverType] { get }

    func send(message: MessageType)

struct Programmer: Receiver {
    let name: String

    init(name: String) { = name

    func receive(message: String) {
        print("\(name) received: \(message)")

final class MessageMediator: Sender {
    internal var recipients: [Programmer] = []

    func add(recipient: Programmer) {

    func send(message: String) {
        for recipient in recipients {
            recipient.receive(message: message)
Enter fullscreen mode Exit fullscreen mode


func spamMonster(message: String, worker: MessageMediator) {
    worker.send(message: message)

let messagesMediator = MessageMediator()

let user0 = Programmer(name: "Linus Torvalds")
let user1 = Programmer(name: "Avadis 'Avie' Tevanian")
messagesMediator.add(recipient: user0)
messagesMediator.add(recipient: user1)

spamMonster(message: "I'd Like to Add you to My Professional Network", worker: messagesMediator)
Enter fullscreen mode Exit fullscreen mode

Sources: Github

In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

🐝 Chain Of Responsibility
👫 Command Pattern
🎶 Interpreter Pattern
🍫 Iterator Pattern
💐 Mediator Pattern
💾 Memento Pattern
👓 Observer Pattern
🐉 State Pattern
💡 Strategy Pattern
📝 Template Method
🏃 Visitor Pattern
🌰 Abstract Factory
👷 Builder Pattern
🏭 Factory Method
🔂 Monostate Pattern
🃏 Prototype Pattern
💍 Singleton
🔌 Adapter Pattern
🌉 Bridge Pattern
🌿 Composite Pattern
🍧 Decorator Pattern
🎁 Facade Pattern
🍃 Flyweight Pattern
Protection Proxy
🍬 Virtual Proxy

I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
📧 Email:
👋 LinkedIn:
👋 LeetCode:
👋 Twitter:
👋 Github:
🌎 Website:
🌎 Reddit:
🌎 Quora:
🌎 Medium:
🖨️ PDF Design Patterns: Download

Top comments (0)