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

Design Patterns · Advanced · question 48 of 100

How can you apply the Command pattern in a message-based architecture to support different message types and their processing?

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

In a message-based architecture, messages are the primary means of communication between various components of the system. These messages can be of different types and may require different processing methods. To support such a system, one can apply the Command pattern.

The Command pattern encapsulates a request or an operation as an object allowing us to parameterize clients with different requests, queue or log requests, and support undoable operations. In a message-based architecture, each message can be represented as a Command object. The Command object contains all the necessary information about the message, including the message type, sender, receiver, and the data payload.

To support different message types and their processing, we can define an interface called ‘Command‘ that defines a single method called ‘execute‘. The ‘Command‘ interface can be implemented by different message processor classes, each responsible for processing a specific message type. Each processor class can take the message payload as a constructor argument, and its‘execute‘ method can implement the processing logic for that message type.

Here’s an example implementation in Java:

// Command interface for different message types
public interface Command {
    void execute();
}

// Concrete command implementation for message of type "Email"
public class EmailCommand implements Command {
    private String from;
    private String to;
    private String subject;
    private String content;

    public EmailCommand(String from, String to, String subject, String content) {
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.content = content;
    }

    @Override
    public void execute() {
        //processing logic for email message
        System.out.println("Sending email from " + from + " to " + to + ", Subject: " + subject + ", Content: " + content);
    }
}

// Concrete command implementation for message of type "SMS"
public class SMSCommand implements Command {
    private String from;
    private String to;
    private String content;

    public SMSCommand(String from, String to, String content) {
        this.from = from;
        this.to = to;
        this.content = content;
    }

    @Override
    public void execute() {
        //processing logic for SMS message
        System.out.println("Sending SMS from " + from + " to " + to + ", Content: " + content);
    }
}


// Invoker class that receives the message and invokes corresponding command to process it.
public class MessageProcessor {
    private Command command;

    public void receiveMessage(Command command) {
        this.command = command;
        this.command.execute();
    }
}

// Client code - the message sender
public class Client {
    public static void main(String[] args) {
        // create message commands
        Command emailCommand = new EmailCommand("user1@gmail.com", "user2@gmail.com", "Test Email", "Hello from user 1!");
        Command smsCommand = new SMSCommand("1234567890", "0987654321", "Hello from user 1!");

        // create message processor and send messages
        MessageProcessor processor = new MessageProcessor();
        processor.receiveMessage(emailCommand);
        processor.receiveMessage(smsCommand);
    }
}

In this example, the ‘Command‘ interface defines a single method called ‘execute‘, which is implemented by the concrete command classes such as ‘EmailCommand‘ and ‘SMSCommand‘. The ‘MessageProcessor‘ class acts as the invoker, accepting the specific command as input and invoking its ‘execute‘ method to process the message. Finally, the ‘Client‘ class creates the appropriate message commands and sends them to the message processor for execution.

By using the Command pattern, we have a flexible and extensible system that can handle different message types with varying processing needs. We can easily add new message types and their corresponding processing logic by creating new concrete command classes and implementing the ‘Command‘ interface.

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