当前位置:网站首页>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 !
边栏推荐
- Why does the path obtained by the remote server echo%temp% have a number of 1, 2, 3, etc
- Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
- How about speech synthesis? Is speech synthesis effective?
- 2、 Shell position variable
- [SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
- Tcapulusdb Jun · industry news collection
- Tencent cloud double 11 Live Room activity rules
- How to set up AI speech synthesis? What is the function of speech synthesis?
- Digital case show ‖ made in China for the first time! Tencent cloud tdsql landed in Zhangjiagang bank and worked together to build financial business
- Echo framework: add tracing Middleware
猜你喜欢

Review of AI hotspots this week: the Gan compression method consumes less than 1/9 of the computing power, and the open source generator turns your photos into hand drawn photos
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

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

application. Yaml configuring multiple running environments

BIM model example
![[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)

layer 3 switch

Stm32g474 infrared receiving based on irtim peripherals

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

How to fill in and register e-mail, and open mass mailing software for free
随机推荐
[actual combat] how to realize people nearby through PostGIS
How to access the server through the fortress machine? What if the fortress cannot access the server?
[seckill] new / old users lock in the "explosive seckill zone" and snap up the one-year validity cloud function resource yyds for 63 yuan!
SAP mm UB type sto cannot be transferred to vendor consignment inventory?
OA system module design scheme
Comparison between rule engine and ML model - xlaszlo
layer 3 switch
Tcapulusdb Jun · industry news collection
No serializer found for class ** and no propert no properties discovered to create BeanSerializer
2021-11-10:o (1) time inserts, deletes and obtains random elements. Implement ra
Thoroughly explain the details of the simple factory that you haven't paid attention to
How to generate EAN13 serial number barcode
6、 Symbols and commands for numerical calculation of variables
Custom form dynamic form form designer process engine design scheme
5、 Build freestyle projects and related knowledge
Global and Chinese alumina nanoparticle market scale and Development Trend Outlook report 2022-2028
A review of Nature Neuroscience: dynamic representation in networked nervous system
How to solve the problem of uncaught (in promise) when easywasmlayer plays a video?
[JS reverse hundred examples] md5+aes encryption analysis of an easy payment password
[tcapulusdb knowledge base] how does tcapulusdb add a business cluster cluster?