This article is excerpted from This is How Design Patterns should Be Learned
For example, when we are in the office, we often communicate with colleagues through email messages, SMS messages, or system messages. Especially when we go through some approval processes, we need to document those processes for future reference. Messages can be classified into email messages, SMS messages, and intra-system messages by type. However, messages can be classified into normal messages, urgent messages, and urgent messages according to the degree of urgency. Obviously, the entire messaging system can be divided into two dimensions, as shown in the figure below.
If we use inheritance, the situation is complicated and not conducive to scaling. Mail messages can be regular or urgent; SMS messages can be regular or urgent. Let’s use bridge mode to solve this problem. Start by creating an IMessage interface to act as a bridge.
/** * implements a unified interface for sending messages */
public interface IMessage {
// The content of the message to be sent and the recipient
void send(String message, String toUser);
}
Copy the code
Create a mail message implementation EmailMessage class.
/** * Mail message implementation class */
public class EmailMessage implements IMessage {
public void send(String message, String toUser) {
System.out.println("Send by mail message" + message + "To"+ toUser); }}Copy the code
Create an SmsMessage implementation SmsMessage class.
/** * Short IMessage Service (SMS) */
public class SmsMessage implements IMessage {
public void send(String message, String toUser) {
System.out.println("Using SMS messaging" + message + "To"+ toUser); }}Copy the code
Then create the bridge abstract role AbstractMessage class.
/** * Abstract message class */
public abstract class AbstractMessage {
// Hold the object of an implementation part
IMessage message;
// Constructor, passing in the object of the implementation part
public AbstractMessage(IMessage message) {
this.message = message;
}
// Send the message, delegate to the implementation part of the method
public void sendMessage(String message, String toUser) {
this.message.send(message, toUser); }}Copy the code
Create a NomalMessage class that implements a plain message.
/** * Ordinary message class */
public class NomalMessage extends AbstractMessage {
// Constructor, passing in the object of the implementation part
public NomalMessage(IMessage message) {
super(message);
}
@Override
public void sendMessage(String message, String toUser) {
// For normal messages, just call the parent method to send the message
super.sendMessage(message, toUser); }}Copy the code
Create the UrgencyMessage class that implements urgent messages.
/** * urgent messages */
public class UrgencyMessage extends AbstractMessage {
// constructor
public UrgencyMessage(IMessage message) {
super(message);
}
@Override
public void sendMessage(String message, String toUser) {
message = "Urgent:" + message;
super.sendMessage(message, toUser);
}
// Extend it to monitor the processing status of a message
public Object watch(String messageId) {
// Query the message processing status according to the given message code (messageId)
// organize into a monitored processing state and return
return null; }}Copy the code
Finally write the client test code.
public static void main(String[] args) {
IMessage message = new SmsMessage();
AbstractMessage abstractMessage = new NomalMessage(message);
abstractMessage.sendMessage("Quick approval of Overtime Application"."Mr.wong.");
message = new EmailMessage();
abstractMessage = new UrgencyMessage(message);
abstractMessage.sendMessage("Quick approval of Overtime Application"."Mr.wong.");
}
Copy the code
The running result is shown in the figure below.
In the above case, we used the bridge pattern to decouple the dimensions of “message type” and “message urgency” that vary independently. If there are more message types, such as wechat and Dingpin, then directly create a class to inherit IMessage; If the degree of urgency needs to be added, then again you just need to create a new class that implements the AbstractMessage class.
Pay attention to “Tom play architecture” reply to “design pattern” can obtain the complete source code.
Tom play architecture: 30 real cases of design patterns (attached source code), the challenge of annual salary 60W is not a dream
This article is “Tom play structure” original, reproduced please indicate the source. Technology is to share, I share my happiness! If this article is helpful to you, welcome to follow and like; If you have any suggestions can also leave a comment or private letter, your support is my motivation to adhere to the creation. Pay attention to “Tom bomb architecture” for more technical dry goods!