当前位置:网站首页>Designing complex messaging systems using bridging patterns

Designing complex messaging systems using bridging patterns

2022-06-24 02:01:00 Tom bomb architecture

This article is excerpted from 《 This is how design patterns should be learned 》

for instance , We often send messages by mail when we work at ordinary times 、 Communicate with colleagues via SMS messages or messages in the system . Especially when going through some approval processes , We need to document these processes for future reference . According to the type , Messages can be divided into mail messages 、 SMS messages and messages in the system . however , Divide according to the degree of urgency , Messages can be divided into ordinary messages 、 Urgent news and special news . obviously , The whole message system can be divided into two dimensions , As shown in the figure below .

file

If we use inheritance , Then the situation is complicated , And it's not conducive to expansion . Mail messages can be ordinary , It can also be urgent ; SMS messages can be ordinary , It can also be urgent . Let's use bridge mode to solve this problem .

First create a IMessage The interface acts as a bridge .

/**
 *  Realize the unified interface of message sending 
 */
public interface IMessage {
    // The content and recipient of the message to be sent 
    void send(String message, String toUser);
}

Create mail message implementation EmailMessage class .

/**
 *  Implementation class of mail message 
 */
public class EmailMessage implements IMessage {
    public void send(String message, String toUser) {
        System.out.println(" Use email messages to send " + message + " to " + toUser);
    }
}

Create SMS message to realize SmsMessage class .

/**
 *  Implementation class of SMS message 
 * SMS(Short IMessage Service) SMS service 
 */
public class SmsMessage implements IMessage {
    public void send(String message, String toUser) {
        System.out.println(" Use SMS messages to send " + message + " to " + toUser);
    }
}

Then create a bridging Abstract role AbstractMessage class .

/**
 *  Abstract message class 
 */
public abstract class AbstractMessage {
    // Objects that hold an implementation 
    IMessage message;

    // Construction method , Objects passed in to the implementation 
    public AbstractMessage(IMessage message) {
        this.message = message;
    }

    // Send a message , The method delegated to the implementation part 
    public void sendMessage(String message, String toUser) {
        this.message.send(message, toUser);
    }
}

Create a concrete implementation of the common message NomalMessage class .

/**
 *  General message class 
 */
public class NomalMessage extends AbstractMessage {

    // Construction method , Objects passed in to the implementation 
    public NomalMessage(IMessage message) {
        super(message);
    }

    @Override
    public void sendMessage(String message, String toUser) {
        // For normal messages , Just call the parent class method to send a message 
        super.sendMessage(message, toUser);
    }
}

Create specific implementation urgent message UrgencyMessage class .

/**
 *  Urgent messages 
 */
public class UrgencyMessage extends AbstractMessage {

    // Construction method 
    public UrgencyMessage(IMessage message) {
        super(message);
    }

    @Override
    public void sendMessage(String message, String toUser) {
        message = " Urgent :" + message;
        super.sendMessage(message, toUser);
    }

    // Expand its functions , Monitor the processing status of a message 
    public Object watch(String messageId) {
        // According to the given message code (messageId) Query the processing status of the message 
        // Organized into monitored processing status , Then return 
        return null;
    }
}

Finally, write client test code .

 public static void main(String[] args) {
        IMessage message = new SmsMessage();
        AbstractMessage abstractMessage = new NomalMessage(message);
        abstractMessage.sendMessage(" Overtime application quick approval ", " Wang Zong ");

        message = new EmailMessage();
        abstractMessage = new UrgencyMessage(message);
        abstractMessage.sendMessage(" Overtime application quick approval ", " Wang Zong ");
}

The results are shown in the following figure .

file

In the case above , We use bridge mode to decouple “ Message type ” and “ The urgency of the news ” These two dimensions of independent change . If there are more message types in the future , For example, wechat 、 Nails, etc , Then directly create a new class inheritance IMessage that will do ; If the urgency needs to be added , Then you just need to create a new class implementation AbstractMessage The class can .

Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “ Design patterns ” The complete source code is available .

【 recommend 】Tom Bomb architecture :30 A real case of design pattern ( Source code attached ), Challenge annual salary 60W Is not a dream

This paper is about “Tom Bomb architecture ” original , Reprint please indicate the source . Technology is about sharing , I share my happiness !undefined If this article helps you , Welcome to pay attention and like ; If you have any suggestions, you can also leave comments or private letters , Your support is the driving force for me to adhere to my creation . Pay attention to WeChat public number 『 Tom Bomb architecture 』 More technical dry goods are available !

原网站

版权声明
本文为[Tom bomb architecture]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211108174741305F.html