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

Design Patterns · Expert · question 77 of 100

Describe a scenario where the Bridge pattern can be combined with the Prototype or Builder patterns to create highly configurable and extensible systems.?

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

The Bridge pattern is used to separate the abstraction from its implementation in order to allow them to vary independently. This pattern is useful when you want to add new functionality or change the implementation without changing the abstraction.

The Prototype pattern is used to create new objects based on a prototype copy, which is an efficient way to create multiple objects with the same attributes/properties. The Builder pattern is used to create complex objects using a step-by-step approach.

One scenario where the Bridge pattern can be combined with the Prototype or Builder patterns is creating a highly configurable and extensible system that involves different messaging protocols such as TCP, HTTP or FTP. In this scenario, we can apply the Bridge pattern to separate the Message abstraction from its implementation, so we could easily switch between different messaging protocols without affecting the Message abstraction.

To create highly configurable and extensible systems, we can use the Prototype or Builder pattern to create different types of messages based on some configurations. For example, we could have a configuration file that specifies the message type, the protocol, and other options. Then, we can use the Prototype pattern to create a prototype message based on the configuration, which can be used to create multiple objects with the same configuration.

Here’s how we can implement this scenario in Java, using the Bridge, Prototype, and Builder patterns:

First, we define the Message interface and its implementation using the Bridge pattern:

public interface Message {
    void send();
}

public abstract class AbstractMessage implements Message {
    protected MessageSender sender;
    protected String message;

    public AbstractMessage(String message, MessageSender sender) {
        this.message = message;
        this.sender = sender;
    }
}

Next, we implement different message senders:

public interface MessageSender {
    void sendMessage(String message);
}

public class TcpMessageSender implements MessageSender {
    public void sendMessage(String message) {
        // Implementation
    }
}

public class HttpMessageSender implements MessageSender {
    public void sendMessage(String message) {
        // Implementation
    }
}

public class FtpMessageSender implements MessageSender {
    public void sendMessage(String message) {
        // Implementation
    }
}

Now, we can use the Builder pattern to create different types of messages based on some configurations:

public interface MessageBuilder {
    MessageBuilder setProtocol(String protocol);
    MessageBuilder setFrom(String from);
    MessageBuilder setTo(String to);
    MessageBuilder setSubject(String subject);
    MessageBuilder setText(String text);

    Message build();
}

public class EmailMessageBuilder implements MessageBuilder {
    private EmailMessage message;

    public EmailMessageBuilder() {
        this.message = new EmailMessage();
    }

    public MessageBuilder setProtocol(String protocol) {
        // Implementation for email protocol
        return this;
    }

    public MessageBuilder setFrom(String from) {
        this.message.setFrom(from);
        return this;
    }

    public MessageBuilder setTo(String to) {
        this.message.setTo(to);
        return this;
    }

    public MessageBuilder setSubject(String subject) {
        this.message.setSubject(subject);
        return this;
    }

    public MessageBuilder setText(String text) {
        this.message.setText(text);
        return this;
    }

    public Message build() {
        return this.message.clone();
    }
}

public class EmailMessage extends AbstractMessage implements Cloneable {
    private String from;
    private String to;
    private String subject;
    private String text;

    public EmailMessage() {
        super("", null);
    }

    public void send() {
        this.sender.sendMessage(message);
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public EmailMessage clone() {
        return new EmailMessage(this.from, this.to, this.subject, this.text, this.sender);
    }

    public EmailMessage(String from, String to, String subject, String text, MessageSender sender) {
        super("", sender);
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
        this.message = "From: " + from + "n"
                + "To: " + to + "n"
                + "Subject: " + subject + "n"
                + text;
    }
}

We can define other builders for different types of messages and protocols, and also use the Prototype pattern to clone existing messages with different configurations rather than creating a new one from scratch.

This approach creates a highly configurable and extensible system that can support different messaging protocols and configurations without affecting the Message abstraction. It also provides a flexible way to create multiple messages with the same configuration using the Prototype pattern, or create complex messages step-by-step using the Builder pattern.

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