Friday, 17 January 2014

Bridge Design pattern - Real time example [Publish Message]

Master Software Architecture & Clean Code!

Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!

SUBSCRIBE NOW

Why Message Publishing Needs the Bridge Pattern

In enterprise systems, message publishing frequently evolves in two separate directions: the type of message (such as text, encrypted, or priority messages) and the delivery mechanism (such as Email, SMS, or Message Queues). Hardcoding communication channels directly into message classes causes severe architectural rigidity and subclass explosion.

The Bridge Design Pattern solves this challenge by decoupling the abstraction (the message contract) from its implementation (the delivery provider), letting both sides scale and evolve independently without impacting one another.

Complete Java Code Walkthrough

Below is a clean, practical Java implementation demonstrating the message publishing architecture using the Bridge pattern:

// 1. Implementor Interface
interface MessagePublisher {
    void publish(String message);
}

// 2. Concrete Implementors
class EmailPublisher implements MessagePublisher {
    @Override
    public void publish(String message) {
        System.out.println("Email Channel: " + message);
    }
}

class SmsPublisher implements MessagePublisher {
    @Override
    public void publish(String message) {
        System.out.println("SMS Channel: " + message);
    }
}

// 3. Abstraction (Holds reference to Implementor)
abstract class Message {
    protected MessagePublisher publisher;

    public Message(MessagePublisher publisher) {
        this.publisher = publisher;
    }

    public abstract void send(String content);
}

// 4. Refined Abstraction
class SystemNotificationMessage extends Message {
    public SystemNotificationMessage(MessagePublisher publisher) {
        super(publisher);
    }

    @Override
    public void send(String content) {
        publisher.publish("[SYSTEM NOTIFICATION] " + content);
    }
}

class AlertMessage extends Message {
    public AlertMessage(MessagePublisher publisher) {
        super(publisher);
    }

    @Override
    public void send(String content) {
        publisher.publish("[CRITICAL ALERT] " + content);
    }
}

// 5. Client Application
public class MessagePublishingDemo {
    public static void main(String[] args) {
        // Publish system notification via Email
        Message notification = new SystemNotificationMessage(new EmailPublisher());
        notification.send("Routine scheduled maintenance at 12:00 AM.");

        // Publish critical alert via SMS
        Message alert = new AlertMessage(new SmsPublisher());
        alert.send("Service latency spike detected!");

        // Dynamically switch delivery channel to Email for alerts
        Message emailAlert = new AlertMessage(new EmailPublisher());
        emailAlert.send("Backup server failover initialized.");
    }
}

Key Architectural Takeaways

  • Loose Coupling: Message types do not need to know low-level details about email protocols, SMS gateways, or cloud APIs.
  • Runtime Flexibility: Delivery channels can be dynamically swapped or configured based on user preference, priority, or environment settings.
  • Clean Extensibility: Introducing a new message type (e.g., AuditLogMessage) or transport provider (e.g., KafkaPublisher) requires zero modifications to existing classes.

Watch More Related Design Pattern Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=BdOLJLhzW6A

Click the below Image to Enlarge
Bridge Design pattern - Real time example [Publish Message]




















See also:

  • Bridge Design pattern - Introduction
  • Bridge Design pattern - Real time example [Send Message]
  • Bridge Design pattern - Real time example [Shape]
  • Bridge Design pattern - Real time example [TV]
  • Bridge Design pattern - Class Diagram
  • Bridge Design pattern - Implementation [Shape]
  • Bridge Design pattern - Implementation [Send Message]
  • Bridge Design pattern - Implementation [Publish Message]
  • Bridge Design pattern - Implementation [LED TV]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials