Design Patterns: Mediator – Harness the Power of Swift

Design Patterns: Mediator – Harness the Power of Swift!

Programming design patterns are an essential tool for any software developer. They provide a way to structure code in a way that is easy to read, understand, and maintain. One of the most powerful design patterns is the Mediator Pattern. This pattern allows objects to communicate with each other without having direct knowledge of each other. In this article, we will explore how to use the Mediator Pattern in Swift.

The Mediator Pattern provides a way for objects to interact with each other without the need for direct knowledge of each other’s internals. The Mediator Pattern is especially useful when dealing with complex systems where there are many objects that need to interact with each other. By using the Mediator Pattern, the complexity of the system can be reduced, as the objects no longer need to know about each other.

The Mediator Pattern consists of two main components: a mediator and a colleague. The mediator acts as the middleman between the colleagues. It is responsible for managing the communication between the colleagues and ensuring that the data is passed correctly. The colleagues are the objects that need to communicate with each other. They do not need to know about each other’s internals.

In Swift, the Mediator Pattern can be implemented using protocols and classes. We will create a protocol called MediatorProtocol which will define the methods that the mediator will need to implement. We will also create a class called Mediator which will implement the MediatorProtocol. The Mediator class will have a list of colleagues and will be responsible for managing the communication between them.

The MediatorProtocol will define two methods: sendMessage and receiveMessage. The sendMessage method will be used by the colleagues to send a message to the mediator. The receiveMessage method will be used by the mediator to receive messages from the colleagues.

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

The Colleague class will represent the objects that need to communicate with each other. It will have a reference to the mediator and will use it to send and receive messages.

class Colleague {
  let mediator: MediatorProtocol
  
  init(mediator: MediatorProtocol) {
    self.mediator = mediator
  }
  
  func sendMessage(message: String) {
    mediator.sendMessage(message: message, sender: self)
  }
  
  func receiveMessage(message: String) {
    print("Received message: \(message)")
  }
}

The Mediator class will implement the MediatorProtocol. It will have an array of colleagues and will be responsible for managing the communication between them. The Mediator class will have two methods: sendMessage and receiveMessage. The sendMessage method will be used by the colleagues to send a message to the mediator. The receiveMessage method will be used by the mediator to receive messages from the colleagues.

class Mediator: MediatorProtocol {
  var colleagues: [Colleague] = []
  
  func addColleague(colleague: Colleague) {
    colleagues.append(colleague)
  }
  
  func sendMessage(message: String, sender: Colleague) {
    for colleague in colleagues {
      if colleague !== sender {
        colleague.receiveMessage(message: message)
      }
    }
  }
  
  func receiveMessage(message: String, sender: Colleague) {
    // Do nothing
  }
}

To use the Mediator Pattern, we first need to create an instance of the Mediator class and add the colleagues to it. Then, when one of the colleagues needs to send a message, it can call the sendMessage method on the mediator, passing the message and the sender. The mediator will then relay the message to all of the other colleagues.

In this article, we explored how to use the Mediator Pattern in Swift. We created a protocol for the mediator and a class for the colleagues. We also created a Mediator class which was responsible for managing the communication between the colleagues. Using the Mediator Pattern, we can structure our code in a way that is easy to read, understand, and maintain.

Scroll to Top