Master Software Architecture & Clean Code!
Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!
SUBSCRIBE NOWWhat is the Bridge Design Pattern?
The Bridge Design Pattern decouples an abstraction from its implementation so that both can evolve independently. Instead of relying on rigid, multi-level class inheritance—which causes an explosion of subclasses—the Bridge pattern connects separate class hierarchies through composition.
In a message publishing or notification system, the type of message being processed (e.g., text, encrypted, or urgent) acts as the abstraction, while the delivery platform (e.g., Email, SMS, or Push Notification) serves as the concrete implementation.
Complete Java Code Implementation
Below is a clean, practical Java implementation illustrating the Bridge pattern with a real-world message publishing architecture:
// 1. Implementor Interface
interface MessageSender {
void sendMessage(String message);
}
// 2. Concrete Implementors
class EmailSender implements MessageSender {
@Override
public void sendMessage(String message) {
System.out.println("Email: Sending message -> " + message);
}
}
class SmsSender implements MessageSender {
@Override
public void sendMessage(String message) {
System.out.println("SMS: Sending text -> " + message);
}
}
// 3. Abstraction (Bridge holding the Implementor reference)
abstract class Message {
protected MessageSender messageSender;
public Message(MessageSender messageSender) {
this.messageSender = messageSender;
}
public abstract void publish(String content);
}
// 4. Refined Abstractions
class TextMessage extends Message {
public TextMessage(MessageSender messageSender) {
super(messageSender);
}
@Override
public void publish(String content) {
messageSender.sendMessage("[Standard Text] " + content);
}
}
class EncryptedMessage extends Message {
public EncryptedMessage(MessageSender messageSender) {
super(messageSender);
}
@Override
public void publish(String content) {
String encryptedContent = new StringBuilder(content).reverse().toString();
messageSender.sendMessage("[Encrypted] " + encryptedContent);
}
}
// 5. Client Application
public class BridgePatternDemo {
public static void main(String[] args) {
// Send a text message via Email
Message emailMessage = new TextMessage(new EmailSender());
emailMessage.publish("Welcome to Ram N Java!");
// Send an encrypted message via SMS
Message smsMessage = new EncryptedMessage(new SmsSender());
smsMessage.publish("TopSecretCode123");
// Swap channels dynamically: Send encrypted content via Email
Message secureEmail = new EncryptedMessage(new EmailSender());
secureEmail.publish("ConfidentialReport2026");
}
}
Key Architectural Advantages
- Prevents Combinatorial Subclass Explosion: Adding 3 message types and 3 delivery channels creates only 6 classes instead of 9 tightly coupled variants.
- Runtime Channel Switching: Delivery mechanisms can be injected dynamically at runtime based on configuration or user preferences.
- Open/Closed Principle: You can add a new messaging format or a new transport channel without modifying existing classes.
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=Q2L3FGnjPu0
Click the below Image to Enlarge
| Bridge Design pattern - Implementation [Publish Message] |
| Bridge Design pattern - Implementation [Publish Message] - Class Diagram |
MessagePublisher.java
public interface MessagePublisher
{
public void publishMessage(String message);
}
No comments:
Post a Comment