Design Patterns: Mediato in Swift – Harnessing the Power of Reusability

Design Patterns: Mediator in Swift – Harnessing the Power of Reusability

Design patterns are essential for developers to understand. They help us create reusable, maintainable, and extensible software. In this blog post, we will explore how to use the mediator design pattern in Swift.

The mediator pattern is a great way to reduce tight coupling between objects. It also helps keep code clean and organized. The mediator pattern provides a centralized point of communication between components. This helps to reduce the number of dependencies between objects, making them easier to maintain and extend.

In Swift, the mediator pattern is implemented using the protocol-oriented programming (POP) approach. POP allows us to define protocols that can be used to implement the mediator pattern. We can then use these protocols to create classes that conform to the mediator pattern.

Let’s take a look at an example of how to implement the mediator pattern in Swift. We’ll start by defining a protocol that will be used to implement the mediator pattern. We’ll call it MediatorProtocol.

 protocol MediatorProtocol {
    func sendMessage(message: String, sender: String)
} 

This protocol defines a single method, sendMessage(), which will be used to pass messages between objects. The message and the sender are both passed as parameters.

Next, we’ll create a class that implements the mediator pattern. We’ll call it Mediator. This class will conform to the MediatorProtocol and will be responsible for sending messages between objects.

 class Mediator: MediatorProtocol {
    var objects = [String: Any]()
    
    func sendMessage(message: String, sender: String) {
        for (key, value) in objects {
            if key != sender {
                let object = value as? MediatorProtocol
                object?.sendMessage(message: message, sender: sender)
            }
        }
    }
    
    func registerObject(object: Any, key: String) {
        objects[key] = object
    }
} 

This class contains two methods, sendMessage() and registerObject(). The sendMessage() method is responsible for sending messages between objects. It loops through all the objects registered with the mediator and sends the message to each one, except the sender.

The registerObject() method is used to register objects with the mediator. This allows the mediator to keep track of all the objects that are registered with it.

Now that we have our mediator set up, let’s take a look at how to use it. We’ll create two classes, ClientA and ClientB, that will be used to send and receive messages. These classes will conform to the MediatorProtocol and will be responsible for sending and receiving messages.

 class ClientA: MediatorProtocol {
    var mediator: MediatorProtocol?
    
    func sendMessage(message: String, sender: String) {
        mediator?.sendMessage(message: message, sender: sender)
    }
}

class ClientB: MediatorProtocol {
    var mediator: MediatorProtocol?
    
    func sendMessage(message: String, sender: String) {
        mediator?.sendMessage(message: message, sender: sender)
    }
} 

These classes contain a single method, sendMessage(), which is used to send messages to other objects.

Finally, we’ll create an instance of the mediator and register our objects with it.

 let mediator = Mediator()
let clientA = ClientA()
let clientB = ClientB()

mediator.registerObject(object: clientA, key: "clientA")
mediator.registerObject(object: clientB, key: "clientB")

clientA.mediator = mediator
clientB.mediator = mediator

clientA.sendMessage(message: "Hello from Client A!", sender: "clientA")
clientB.sendMessage(message: "Hello from Client B!", sender: "clientB") 

In the code above, we create an instance of the mediator and register our objects with it. We then set the mediator property on each of the objects and call the sendMessage() method on each one.

The mediator pattern is a powerful tool for reducing tight coupling between objects. By using the protocol-oriented programming approach, we can easily implement the mediator pattern in Swift. This makes our code cleaner, more organized, and easier to maintain and extend.

Scroll to Top