Design Patterns: Mediator – Using Swift to Create More Efficient Apps

Design Patterns: Mediator – Using Swift to Create More Efficient Apps

Good software design is essential for creating apps that are efficient and easy to maintain. Design patterns are reusable solutions to common software design problems. One of the most popular design patterns is the Mediator pattern, which helps to reduce coupling between objects in an application. In this blog post, we’ll explore how to use the Mediator pattern with Swift to create more efficient apps.

The Mediator pattern is a behavioral design pattern that provides a centralized hub for communication between objects. It allows objects to communicate with each other without having to know about each other’s details. This reduces coupling between objects and makes it easier to add new objects to the system.

Let’s look at an example of how the Mediator pattern can be used in a Swift app. We’ll create a simple chat application that has two users, Alice and Bob. Each user will have their own view controller and view model. The view models will communicate with each other through the mediator object.

First, we’ll create the mediator class. This class will manage all of the communication between the view models.

class Mediator {
    
    private var aliceViewModel: AliceViewModel!
    private var bobViewModel: BobViewModel!
    
    func setAliceViewModel(viewModel: AliceViewModel) {
        self.aliceViewModel = viewModel
    }
    
    func setBobViewModel(viewModel: BobViewModel) {
        self.bobViewModel = viewModel
    }
    
    func sendMessage(message: String) {
        aliceViewModel.receivedMessage(message: message)
        bobViewModel.receivedMessage(message: message)
    }
}

Next, we’ll create the view models. The view models will be responsible for handling the communication with the mediator object.

class AliceViewModel {
    
    private var mediator: Mediator!
    
    func setMediator(mediator: Mediator) {
        self.mediator = mediator
    }
    
    func sendMessage(message: String) {
        mediator.sendMessage(message: message)
    }
    
    func receivedMessage(message: String) {
        print("Alice received: \(message)")
    }
}

class BobViewModel {
    
    private var mediator: Mediator!
    
    func setMediator(mediator: Mediator) {
        self.mediator = mediator
    }
    
    func sendMessage(message: String) {
        mediator.sendMessage(message: message)
    }
    
    func receivedMessage(message: String) {
        print("Bob received: \(message)")
    }
}

Finally, we’ll create the view controllers. The view controllers will be responsible for handling the UI for the users.

class AliceViewController: UIViewController {
    
    private var viewModel: AliceViewModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mediator = Mediator()
        
        let aliceViewModel = AliceViewModel()
        aliceViewModel.setMediator(mediator: mediator)
        mediator.setAliceViewModel(viewModel: aliceViewModel)
        
        let bobViewModel = BobViewModel()
        bobViewModel.setMediator(mediator: mediator)
        mediator.setBobViewModel(viewModel: bobViewModel)
        
        self.viewModel = aliceViewModel
    }
    
    @IBAction func sendMessage(_ sender: Any) {
        let message = "Hello!"
        viewModel.sendMessage(message: message)
    }
}

class BobViewController: UIViewController {
    
    private var viewModel: BobViewModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mediator = Mediator()
        
        let aliceViewModel = AliceViewModel()
        aliceViewModel.setMediator(mediator: mediator)
        mediator.setAliceViewModel(viewModel: aliceViewModel)
        
        let bobViewModel = BobViewModel()
        bobViewModel.setMediator(mediator: mediator)
        mediator.setBobViewModel(viewModel: bobViewModel)
        
        self.viewModel = bobViewModel
    }
    
    @IBAction func sendMessage(_ sender: Any) {
        let message = "Hi there!"
        viewModel.sendMessage(message: message)
    }
}

Now that we’ve created all of the necessary classes, we can put them all together to create our chat application. When Alice sends a message, it will be sent to the mediator object, which will then forward the message to Bob’s view model. Similarly, when Bob sends a message, it will be sent to the mediator object, which will then forward the message to Alice’s view model.

Using the Mediator pattern with Swift allows us to create more efficient apps by reducing coupling between objects. This pattern also makes it easier to add new objects to the system as they can easily communicate with existing objects through the mediator.

In this blog post, we’ve explored how to use the Mediator pattern with Swift to create more efficient apps. We looked at how the Mediator pattern can be implemented in a Swift app and how it can reduce coupling between objects. We also looked at how the Mediator pattern makes it easier to add new objects to the system.

Scroll to Top