当前位置:网站首页>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 .
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 .
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 .
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 !
边栏推荐
- LeetCode 120. Triangle minimum path sum
- What if the cloud desktop fails to connect to the server? How to use cloud desktop?
- Oracle sqlldr quick import and sqluldr2 quick export
- How does the education industry realize the TRTC interactive classroom apaas solution under the "double reduction policy"?
- 3、 Shell variable substring
- Five things programmers need to consider when developing with low code – thenewstack
- A review of Nature Neuroscience: dynamic representation in networked nervous system
- Web user experience design promotion practice
- What is the difference between code signing certificates? What is the use of code signing certificates?
- What is the relationship between cloud desktop and cloud server? How to understand the relationship between the two
猜你喜欢

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

application. Yaml configuring multiple running environments

layer 3 switch
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

Stm32g474 infrared receiving based on irtim peripherals

BIM model example

163 mailbox login portal display, enterprise mailbox computer version login portal

Introduction to development model + test model
随机推荐
Can the fortress machine connect to the ECS? What are the reasons why the fortress cannot connect to the ECS?
Global and Chinese micro focus X-ray tube industry competitiveness analysis and Investment Strategy Research Report 2022-2028
Glusterfs version 4.1 selection and deployment
MySQL architecture
Research Report on global and Chinese titanium concentrate market scale and investment prospects 2022-2028
Detailed explanation of SSH tunnel and stable intranet penetration using autossh
Custom form dynamic form form designer process engine design scheme
Kubesphere upgrade & enable plug-ins after installation
I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
How to set up a cloud desktop server? What are the advantages of cloud desktop?
SAP mm maintains inter company sto error -no delivery type defined for supplying
How does the education industry realize the TRTC interactive classroom apaas solution under the "double reduction policy"?
Using nginscript as a file distribution service with permission
How to design cloud desktop server? What is the future of cloud desktop?
[technical grass planting] how to batch check your server status? Cloud probe panel setup tutorial
[planting grass by technology] three big gifts prepared by Tencent cloud for you on the double 11, welcome to touch~
[tcapulusdb knowledge base] common problems of tcapulusdb local deployment
Echo framework: add tracing Middleware
Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
Collation of commonly used glusterfs commands