WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Guru · question 95 of 100

Analyze the role of the Mediator pattern in implementing complex coordination and orchestration tasks in distributed systems, such as service mesh or workflow engines.?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

The Mediator pattern is a behavioral pattern that promotes loose coupling between objects by encapsulating the interactions among them. In a distributed system, where there are multiple components running on different nodes, coordination and orchestration tasks can become complex and challenging to manage. The Mediator pattern can help alleviate these challenges by providing a centralized component that facilitates communication and collaboration among the different components.

In a service mesh architecture, the Mediator pattern can be used to implement a control plane that manages the behavior of the network of services. The control plane acts as a mediator between the services, handling tasks such as service discovery, load balancing, and traffic management. For example, in Istio, the service mesh platform, the Istio Pilot component acts as a mediator between the services, managing traffic routing and configuring the Envoy proxies that are deployed alongside the services.

Similarly, in a workflow engine, the Mediator pattern can be used to coordinate the activities of the different workflow components. The workflow engine acts as a mediator between the workflow tasks, handling tasks such as task scheduling, error handling, and data management. For example, the Apache Airflow workflow engine provides a DAG (Directed Acyclic Graph) scheduler that acts as a mediator between the tasks in the workflow, managing the dependencies between them and executing them in the correct order.

Java Code Example:

public interface Mediator {
   void send(String message, Component sender);
}

public abstract class Component {
   private Mediator mediator;

   public Component(Mediator mediator) {
      this.mediator = mediator;
   }

   public void send(String message) {
      mediator.send(message, this);
   }

   public abstract void receive(String message);
}

public class ConcreteComponent extends Component {
   public ConcreteComponent(Mediator mediator) {
      super(mediator);
   }

   public void receive(String message) {
      System.out.println("Received message: " + message);
   }
}

public class ConcreteMediator implements Mediator {
   private ConcreteComponent component1;
   private ConcreteComponent component2;

   public void setComponent1(ConcreteComponent component1) {
      this.component1 = component1;
   }

   public void setComponent2(ConcreteComponent component2) {
      this.component2 = component2;
   }

   public void send(String message, Component sender) {
      if (sender.equals(component1)) {
         component2.receive(message);
      } else {
         component1.receive(message);
      }
   }
}

In this example, we have a Mediator interface and a Component abstract class that define the Mediator pattern. ConcreteComponent and ConcreteMediator are concrete implementations of the Mediator pattern. The ConcreteMediator acts as a mediator between two ConcreteComponents, executing the send() method when a component sends a message. The ConcreteComponent receives messages via its receive() method. This shows how the Mediator pattern can be used to manage communication among distributed components in a system.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics