当前位置:网站首页>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 !
边栏推荐
- Wechat open platform: OpenAPI, cloud development and basic management capability upgrade
- Tencent cloud double 11 Live Room activity rules
- Can the server be restarted through the fortress machine? How are the fortress machines connected to the server
- 4、 Variable assignment method
- [tcapulusdb knowledge base] tcapulusdb introduction Questions Summary
- Research Report on global and Chinese titanium concentrate market scale and investment prospects 2022-2028
- How to set up AI speech synthesis? What is the function of speech synthesis?
- Based on ARM embedded real-time streaming media service development and deployment, easygbs supports arm64 architecture
- Easycvr's use of Huawei IVS query directory shared information list interface
- [tcapulusdb knowledge base] how to rebuild tables in tcapulusdb table management?
猜你喜欢

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations
![[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)

application. Yaml configuring multiple running environments

How to fill in and register e-mail, and open mass mailing software for free

layer 3 switch

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

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

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

Stm32g474 infrared receiving based on irtim peripherals
![[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)
随机推荐
[planting grass by technology] three big gifts prepared by Tencent cloud for you on the double 11, welcome to touch~
NFS operations and deployment
What is the cloud desktop server configuration? What are the application scenarios of cloud desktop?
How to determine whether easycvr local streaming media is started successfully?
[tcapulusdb knowledge base] how to get started with tcapulus SQL driver?
How about speech synthesis? Is speech synthesis effective?
Can the server be restarted through the fortress machine? How are the fortress machines connected to the server
Thoroughly explain the details of the simple factory that you haven't paid attention to
What does fortress server mean? What are the benefits of Fortress server installation
How does SAP retail view which Po the allocation table is created with reference to?
How to build a video website? How much does it cost to build a video website?
Benchmarking Shopify? Similarities and differences between "two giants" of Chinese e-commerce SAAS and Weimeng
8、 Pipeline pipeline construction project
Super parameter tuning of neural network using keras tuner
Implementing cos signature with postman
Intensive use of glusterfs 4.1
SAP WM displays the standard report lx09 of TR item
Stm32g474 infrared receiving based on irtim peripherals
How to query the cloud desktop server address? What are the advantages of using cloud desktop?
Tcapulusdb Jun · industry news collection