Bridge Mode of grinding design mode-2

Source: Internet
Author: User
Document directory
  • 2.1 Bridge Mode to solve
  • 2.2 schema structure and description
  • 2.3 sample code for bridging mode
  • 2.4 example of using Bridge Mode Rewriting
2 solution 2.1 Bridge Mode

A reasonable solution for solving the above problems is to use the bridge mode. So what is the bridge mode?

(1) definition of the Bridge Mode
Separate the abstract part from its implementation part so that they can all change independently.

 

(2) Application bridging
Based on the functional requirements of the preceding example, the changes in the example have two latitudes. One latitude is the abstract message side, including common messages, urgent messages, and urgent messages, these abstract messages have a certain relationship, and urgent messages and special messages expand the common messages. The other latitude lies in the specific message sending method, these include intra-site short messages, Email messages, and mobile phone Short Messages. These methods are equal and can be switched. The two latitudes can combine nine different possibilities. Their relationships are shown in figure 5:


Figure 5 combination of message sending possibilities

The root cause of the problem is that the abstract and implementation of messages are mixed, which leads to a change in one latitude, which will lead to a corresponding change in the other latitude, this makes program expansion very difficult.
To solve this problem, we must separate the two latitudes, that is, separating the abstract and implementation parts so that they are independent of each other. In this way, we can implement independent changes and make expansion easier.
The Bridge Mode separates the implementation part from the system by introducing the implemented interface. So, how can we use the specific implementation in the abstract side? It must have been programmed for the Implementation interface. In order to make the abstract side easy to combine with the implementation, the top-level abstract interface should be changed to an abstract class, an instance that contains a specific implementation part.
In this way, for the client that needs to send a message, you only need to create the corresponding message object and then call the method of this message object, the message object calls the real message sending method to send the message. That is to say, the client only wants to send messages and does not want to care about how to send messages.

2.2 schema structure and description

The following figure shows the structure of the bridge mode:

 

Figure 6 structure of the Bridge Mode
Invalid action:
Abstract part of the interface. In this object, we usually need to maintain an object reference for the implementation part. For methods in the abstract object, we need to call the implementation part of the object to complete it. The methods in this object are usually related to specific services.
Refinedaskaction:
Extends the abstract Part of the interface, usually in these objects, defines the methods related to the actual business, the implementation of these methods usually use the method defined in the action, you may also need to call the implementation part of the object to complete the process.
Implementor:
Define the implementation interface. This interface does not need to be consistent with the methods in the action. Generally, the Implementor interface provides basic operations, and the Action defines the business methods based on these basic operations, that is to say, Action defines high-level operations based on these basic operations.
ConcreteImplementor:
Objects that truly implement the Implementor interface.

2.3 sample code for bridging mode

(1) Let's take a look at the Implementor interface definition. The sample code is as follows:

/*** Defines the implementation interface, which can be different from the methods of the abstract interface */public interface Implementor {/*** example method, some specific functions required to implement the abstraction part */public void operationImpl ();}
(2) Let's look at the definition of the define action interface. Note that although it is an interface definition, it is actually implemented as an abstract class. The sample code is as follows:
/*** Define the abstract Part of the interface */public abstract class into action {/*** hold an object for the implementation part */protected Implementor impl;/*** constructor, input the object * @ param impl for implementation */public writable action (Implementor impl) {this. impl = impl;}/*** example operation to implement certain functions. You may need to call the specific implementation method */public void operation () {impl. operationImpl ();}}
(3) let's take a look at the specific implementation. The sample code is as follows:
/*** Real implementation object */public class ConcreteImplementorA implements Implementor {public void operationImpl () {// real Implementation }}
In addition, the sample code is as follows:
/*** Real implementation object */public class ConcreteImplementorB implements Implementor {public void operationImpl () {// real Implementation }}
(4) let's take a look at the object implementation of the extended action interface. The sample code is as follows:
/*** Extends the interface function defined by the specified action */public class RefinedAbstraction extends action {public RefinedAbstraction (Implementor impl) {super (impl);}/*** example operation, implement certain functions */public void otherOperation () {// implement certain functions, and may use specific implementation methods, // but this method is more likely to use the method defined in the action, // use the method defined in the action to complete more functions }}

 

2.4 example of using Bridge Mode Rewriting

After learning the basics of the bridge mode, we should use the bridge mode to rewrite the previous example. Let's take a look at the example to see if using the Bridge Mode to implement the same function can solve the problem of "convenient Implementation of functions and good scalability.
To use the bridge mode to re-implement the preceding example, the first task is to separate the abstract part and the implementation part and analyze the functions to be implemented, the abstract part is the function corresponding to each message type, and the Implementation part is the various message sending methods.
Secondly, define the interfaces for the abstract part and the implementation part according to the structure of the bridge mode, and then implement them separately.
1: starting with simple functions
Starting from a relatively simple function, the functions of common messages and urgent messages are implemented first, and the sending methods are short messages and emails in the station.
The program structure that uses the Bridge Mode to implement these features 7 shows


Figure 7 program structure of a simple function example using the Bridge Mode
Let's look at the code implementation to make it clearer.
(1) first, let's take a look at the implementation of some defined interfaces. The sample code is as follows:

/***** Unified interface for sending messages */public interface MessageImplementor {/***** message sending ** @ param message the message content to be sent * @ param toUser the target person who sends the message */public void send (String message, string toUser );}

(2) Let's look at the abstract Part of the defined interface. The sample code is as follows:

/*** Abstract message object */public abstract class AbstractMessage {/*** holds an object for the implementation part */protected MessageImplementor impl;/*** constructor, input the object * @ param impl for implementation */public AbstractMessage (MessageImplementor impl) {this. impl = impl;}/*** send a message, transfer Implementation Method * @ param message the content of the message to be sent * @ param toUser the recipient of the message sent */public void sendMessage (String message, String toUser) {this. impl. send (message, toUser );}}

(3) let's take a look at how to send messages. Let's take a look at the implementation of short messages in the station. The sample code is as follows:

/*** Send messages via short messages on the site */public class MessageSMS implements MessageImplementor {public void send (String message, String toUser) {System. out. println ("Send message '" + message + "' to" + toUser "by using short messages in the station );}}

Let's look at the implementation of the Email method. The sample code is as follows:

/*** Send messages by Email */public class MessageEmail implements MessageImplementor {public void send (String message, String toUser) {System. out. println ("Send message by Email '" + message + "' to" + toUser );}}

(4) let's take a look at how to extend the abstract message interface. First, let's look at the implementation of common messages. The sample code is as follows:

Public class CommonMessage extends actmessage {public CommonMessage (MessageImplementor impl) {super (impl);} public void sendMessage (String message, String toUser) {// do nothing for common messages, directly call the method of the parent class and send the message to super. sendMessage (message, toUser );}}

Let's take a look at the implementation of the urgent message. The sample code is as follows:

Public class UrgencyMessage extends actmessage {public UrgencyMessage (MessageImplementor impl) {super (impl);} public void sendMessage (String message, String toUser) {message = "urgent:" + message; super. sendMessage (message, toUser);}/*** extends its new functions: monitor the processing process of a message * @ param messageId the number of the message to be monitored * @ return contains the monitored data object, so the Object */public Object watch (String messageId) {// gets the corresponding data, organizes it into a monitored data Object, and then returns return null ;}}

 

2: Add function
After reading the above implementation, it is not very difficult to use the bridge mode to implement it. The key is to see if it can solve the problem mentioned above. Then let's add the unimplemented functions, add the handling of urgent messages, and add a method for sending messages using a mobile phone. How can this problem be achieved?
It is very simple. You only need to add a class for the special message in the abstract Part. by extending the abstract message, you can add the processing function of the special message to the system; the method of adding a mobile phone to send messages is also very simple. In the implementation section, an implementation class is added to implement the method of sending messages by mobile phone.
So simple? It seems that there is no problem mentioned above. Indeed, after the bridge mode is adopted, the abstract part and the implementation part are separated and can be changed independently of each other without affecting each other. Therefore, adding new message processing in the abstract Part does not affect the implementation of message sending. In turn, adding a message sending method does not affect message processing.
(1) Next, let's look at the code implementation. First, let's look at the processing class of the new urgent message. The sample code is as follows:

Public class SpecialUrgencyMessage extends AbstractMessage {public SpecialUrgencyMessage (MessageImplementor impl) {super (impl);} public void hurry (String messageId) {// execute the urged business, message urging} public void sendMessage (String message, String toUser) {message = "urgent:" + message; super. sendMessage (message, toUser); // you need to add a message to be pushed }}

(2) Let's take a look at the implementation of sending messages using short messages on the mobile phone. The sample code is as follows:

/*** Send messages via short message */public class MessageMobile implements MessageImplementor {public void send (String message, String toUser) {System. out. println ("Send message '" + message + "' to" + toUser) Using Short message on the mobile phone );}}

 

3: test the function.

After reading the above implementation, you may feel that it is so easy to add new message processing or send new messages after using the Bridge Mode to implement the previous example, but is this implementation easy to use? Write a client for testing and understanding. The sample code is as follows:

Public class Client {public static void main (String [] args) {// create a specific implementation object MessageImplementor impl = new MessageSMS (); // create a common message object AbstractMessage m = new CommonMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li"); // create an emergency message object m = new UrgencyMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li"); // create an urgent message object m = new SpecialUrgencyMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li"); // switch the implementation method to a short message, and then implement impl = new MessageMobile () again (); m = new CommonMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li"); m = new UrgencyMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li"); m = new SpecialUrgencyMessage (impl); m. sendMessage ("please have a cup of tea", "Xiao Li ");}}

The running result is as follows:

Use the station short message method to send the message 'Please have a cup of tea 'to Xiao Li using the station short message method to send the message 'urgency: please have a cup of tea 'send a message to Xiao Li using the station Short Message' urgent: please have a cup of tea 'to Xiao Li using the short message via mobile phone, send a message 'Please have a cup of tea 'to Xiao Li using the short message method, send the message 'urgent: please have a cup of tea' to Xiao Li using the short message method, send a message 'urgent: please have a cup of tea 'to Xiao Li

 

The first three are intra-site short messages, and the last three are short messages on mobile phones, which correctly implement the expected functions. It seems that the previous implementation should be correct and can complete functions and be flexibly expanded.

 

 

 

 

To be continued 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.