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

Design Patterns · Intermediate · question 35 of 100

Explain the role of the Mediator pattern in reducing coupling between components in a GUI framework.?

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

The Mediator pattern is a behavioral design pattern that allows for the communication between a set of objects to be mediated by a single object, called the Mediator. This pattern is suitable when a system involves many objects that interact in complex ways, creating unwanted dependencies between them. By introducing a Mediator object to handle this communication, we can reduce the coupling between components and improve the maintainability and modifiability of the system.

In a GUI framework, there are often many different components that need to communicate with each other. For example, consider a simple window with a button and a text area. When the button is pressed, the text area should display a message. Without a Mediator object, the button would need to directly manipulate the text area. This can lead to a tight coupling between the button and the text area, making the system hard to modify and maintain.

Using the Mediator pattern, we can introduce a mediator object that handles the communication between the button and the text area. The button would send a message to the mediator indicating that it was pressed, and the mediator would then update the text area with the appropriate message. This approach decouples the button from the text area, allowing them to be modified independently of each other.

Here is an example of how the Mediator pattern could be used in a GUI framework, using Java code. We will create a simple window with a button and a text area, and use a Mediator object to handle the communication between them:

// Define the Mediator interface
interface Mediator {
    void buttonPressed();
}

// Define the Button class
class Button {
    private Mediator mediator;
    
    public Button(Mediator mediator) {
        this.mediator = mediator;
    }
    
    public void press() {
        mediator.buttonPressed();
    }
}

// Define the TextArea class
class TextArea {
    public void displayMessage(String message) {
        System.out.println(message);
    }
}

// Define the Window class that uses the Mediator pattern
class Window implements Mediator {
    private Button button;
    private TextArea textArea;
    
    public Window() {
        textArea = new TextArea();
        button = new Button(this);
    }
    
    public void buttonPressed() {
        textArea.displayMessage("Button was pressed");
    }
    
    public void show() {
        // Code to display the window
        button.press();
    }
}

// Usage example
Window window = new Window();
window.show();

In this example, we have defined a Mediator interface that defines the method for handling button presses. The Button class takes in a Mediator object during construction and calls the ‘buttonPressed()‘ method on it when the button is pressed. The TextArea class simply displays messages.

The Window class acts as the Mediator object, implementing the Mediator interface. When the Window is created, it creates the Button and TextArea objects, passing in ‘this‘ as the Mediator. When the button is pressed, the ‘buttonPressed()‘ method is called on the Window, which in turn updates the TextArea with the appropriate message.

By using a Mediator object to handle the communication between the button and the text area, we have reduced the coupling between these components, making the system easier to modify and maintain.

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