The bridge mode is the object structure mode. It is also known as the Handle and Body mode or Interface mode. The purpose of the bridge mode is to "decouple abstract actions and Implementation so that the two can change independently ".
________________________________________
Intention of the Bridge Model
Although the bridge mode is not a frequently used mode, it is helpful to understand the object-oriented design principles, including the "open-closed" principle and the combination/aggregation Reuse Principle. Understanding these two principles helps to form correct design ideas and cultivate a good design style.
The purpose of the bridge mode is to "decouple abstract actions and Implementation so that the two can change independently ". This sentence is short, but for the first time, people who read this sentence may think for a long time and cannot understand what they mean.
This sentence has three keywords: abstraction, implementation, and decoupling. Understanding the concepts represented by these three words is the key to understanding the intention of the bridge model.
• Abstraction
Extracting common and essential features from numerous things and discarding non-essential features is abstract. For example, apple, bananas, raw pears, peaches, and so on. They share the same characteristics as fruits. The process of getting the fruit concept is an abstract process. To be abstract, you must compare them. If there is no comparison, you cannot find a common part. Common features are the features that distinguish a class of things from other things. These differentiated features are also called essential features. Therefore, the common feature of extracting things is to extract essential features of things and discard non-essential features. Therefore, abstraction is also a process of cropping. In the process of abstraction, the same and different parameters depend on the abstract. The abstract angle depends on the purpose of analyzing the problem.
Normally, if a group of objects have the same features, they can be described through a common class. If some classes have the same features, they can often be described through a common abstract class.
• Implementation
The specific implementation given in abstraction is implementation.
An instance of a class is the instantiation of this class. A specific subclass is the instantiation of its abstract superclass.
• Decoupling
Coupling is a strong association between two entities. Removing their strong associations is the relief of coupling, or decoupling. Here, decoupling means to free up coupling between abstraction and reality, or change strong associations between them into weak associations.
The so-called strong association is the association that has been determined during the compilation period and cannot be dynamically changed during the runtime; the so-called weak Association, is the association that can be dynamically determined and can be dynamically changed during the running period. Obviously, in Java, the inheritance relationship is strongly correlated, while the aggregation relationship is weak Association.
Change the inheritance relationship between two roles to an aggregation relationship, that is, change the strong association between them to a weak Association. Therefore, the so-called decoupling in the bridge mode refers to the use of aggregation rather than inheritance between the abstraction and implementation of a software system, so that the two can change relatively independently. This is the purpose of the bridge model.
Structure of the Bridge Mode
As shown in the following figure, the schematic structure of a schematic system that implements the bridge mode is as follows:
We can see that this system has two levels:
1. abstract level structure composed of abstract roles and corrected abstract roles.
2. Implementation level structure composed of implementation roles and two specific implementation roles.
The following roles are involved in the Bridge Mode:
● Abstract action role: the definition provided in Abstraction and a reference to the implemented object is saved.
● Modify the abstract role: Expand the abstract role, and change and modify the abstract definition of the parent class.
● Implementor role: this role provides an interface for implementing the role, but does not provide a specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of abstract roles. In fact, these two interfaces can be very different. An implemented role should only provide underlying operations, while an abstract role should only provide operations at a higher level based on underlying operations.
● ConcreteImplementor: this role provides the specific implementation of the role interface.
Abstract roles are like the handles of a cup, while realization roles and specific implementation roles are like cups of cups. The handle controls the cup body, which is the source of the "handle body" alias in this mode.
Objects encapsulate behaviors, and actions are implemented by methods. In this schematic system, classes in the abstract hierarchical structure encapsulate the operation () method, while classes in the implementation hierarchical structure encapsulate the operationImpl () method. Of course, there are usually more than one method in the actual system.
Methods In the abstract hierarchical structure can delegate their own functions to the corresponding implemented object, which means that the abstract role can delegate to different implemented objects, to dynamically convert your functions.
Source code
Abstract corner class, which declares a method operation () and provides its implementation. This implementation is implemented by delegating the implemented object (calling the operationImpl () method.
[Java]
1. public abstract class implements action {
2.
3. protected Implementor impl;
4.
5. public role action (Implementor impl ){
6. this. impl = impl;
7 .}
8. // Sample Method
9. public void operation (){
10.
11. impl. operationImpl ();
12 .}
13 .}
Modify abstract roles
[Java]
1. public class refined?action extends =action {
2.
3. public RefinedAbstraction (Implementor impl ){
4. super (impl );
5 .}
6. // other operations
7. public void otherOperation (){
8.
9 .}
10 .}
Implementation roles
[Java]
1. public abstract class Implementor {
2 ./**
3. * Sample method to implement some specific functions required by the abstract Part
4 .*/
5. public abstract void operationImpl ();
6 .}
Specific implementation roles
[Java]
1. public class ConcreteImplementorA extends Implementor {
2.
3. @ Override
4. public void operationImpl (){
5. // specific operation
6 .}
7.
8 .}
[Java]
1. public class ConcreteImplementorB extends Implementor {
2.
3. @ Override
4. public void operationImpl (){
5. // specific operation
6 .}
7.
8 .}
Generally, each method in an implemented role should have a method corresponding to one of the abstract roles, but the opposite is not necessarily true. In other words, the abstract role interface is wider than the implemented role interface. Abstract roles not only provide methods related to the implemented roles, but also provide other methods. The implemented roles are often used only to implement the behaviors related to the abstract roles.
________________________________________
Use Cases
Consider the actual business function of sending a prompt message. Basically, all systems with business process processing have such functions. For example, if there is an unfinished file on the OA, you need to send a message to remind him.
From the business perspective, messages are divided into common messages, urgent messages, and urgent messages. Different types of messages have different service functions. For example, a urgent message is added to a message, in addition to adding urgent messages, a reminder record will be made, and the reminder will be continued in the short time; from the aspect of message sending means, there are also system short messages, mobile phone Short Messages, emails, and so on.
Use the Bridge Mode to solve the problem
According to the functional requirements of the business, business changes have two dimensions. One Dimension is abstract messages, including common messages, urgent messages, and urgent messages, these abstract messages have a certain relationship, and the urgent messages and urgent messages expand the common messages. The other dimension is the specific message sending method, including short messages, emails, and short messages in the system. These methods are equal and can be switched.
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 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 abstract part is the function corresponding to each message type, and the Implementation part is the various message sending methods. Define the interfaces for the abstract part and the implementation part according to the structure of the bridge mode, and then implement them separately.
Source code
Abstract message class
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:26:06
6 .*
7. * @ Class description: Abstract message class
8 .*/
9. public abstract class implements actmessage {
10. // an object holding an implementation Part
11. MessageImplementor impl;
12.
13 ./**
14. * constructor, which is used to pass in the implemented object
15 .*
16. * @ param impl
17. * partial objects
18 .*/
19. public synchronized actmessage (MessageImplementor impl ){
20. this. impl = impl;
21 .}
22.
23 ./**
24. * how to send a message and assign it to the Implementation part
25 .*
26. * @ param message
27. * content of the message to be sent
28. * @ param toUser
29. * message recipient
30 .*/
31. public void sendMessage (String message, String toUser ){
32. this. impl. send (message, toUser );
33 .}
34 .}
Common Message class
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:27:22
6 .*
7. * @ Class description: Common Message class
8 .*/
9. public class CommonMessage extends AbstractMessage {
10.
11. public CommonMessage (MessageImplementor impl ){
12. super (impl );
13 .}
14.
15. @ Override
16. public void sendMessage (String message, String toUser ){
17. // for common messages, directly call the parent class method to send messages.
18. super. sendMessage (message, toUser );
19 .}
20 .}
Urgent message
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:27:43
6 .*
7. * @ Class description: Urgent message class
8 .*/
9. public class UrgencyMessage extends AbstractMessage {
10.
11. public UrgencyMessage (MessageImplementor impl ){
12. super (impl );
13 .}
14.
15. @ Override
16. public void sendMessage (String message, String toUser ){
17. message = "urgent:" + message;
18. super. sendMessage (message, toUser );
19 .}
20.
21 ./**
22. * expand your new functions to monitor the processing status of a message.
23 .*
24. * @ param messageId
25. * Number of the monitored message
26. * @ return the processing status of the monitored message
27 .*/
28. public Object watch (String messageId ){
29. // obtain the status of the message based on the Message id, organize it into a monitored data object, and then return
30. return null;
31 .}
32 .}
Unified interface for sending messages
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:28:04
6 .*
7. * @ Class description: unified interface for sending messages
8 .*/
9. public interface MessageImplementor {
10 ./**
11. * send messages
12 .*
13. * @ param message
14. * content of the message to be sent
15. * @ param toUser
16. * message recipient
17 .*/
18. public void send (String message, String toUser );
19 .}
Implementation of short messages in the system
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:28:25
6 .*
7. * @ Class description: Implementation class of short messages in the system
8 .*/
9. public class MessageSMS implements MessageImplementor {
10.
11. @ Override
12. public void send (String message, String toUser ){
13.
14. System. out. println ("use the short message method in the System to send the message '" + message + "' to" + toUser );
15 .}
16.
17 .}
Implementation of Short Message
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:28:44
6 .*
7. * @ Class description: Implementation class of Short Message
8 .*/
9. public class MessageEmail implements MessageImplementor {
10.
11. @ Override
12. public void send (String message, String toUser ){
13. System. out. println ("use the short message method to send the message '" + message + "' to" + toUser );
14 .}
15.
16 .}
Client type
[Java]
1. package com. bankht. Bridge. bridgeMessage;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 05:29:05
6 .*
7. * @ Class description: client class
8 .*/
9. public class Client {
10.
11. public static void main (String [] args ){
12. // create a specific implementation object
13. MessageImplementor impl = new MessageSMS ();
14. // create a common message object
15. AbstractMessage message = new CommonMessage (impl );
16. message. sendMessage ("overtime application quick approval", "Li Zong ");
17.
18. // switch the implementation method to mail and send it again
19. impl = new MessageEmail ();
20. // create an urgent message object
21. message = new UrgencyMessage (impl );
22. message. sendMessage ("overtime application quick approval", "Li Zong ");
23 .}
24.
25 .}
Observe the above example and you will find that the abstract part and the implementation part are separated and can be changed independently of each other without affecting each other. Therefore, adding a new message processing (Urgent message) in the abstract Part does not affect the implementation of message sending. In turn, the method for sending messages (Short Messages on mobile phones) is added ), message Processing is not affected.
Advantages of the Bridge Mode
● Separating abstraction and implementation
The Bridge Mode separates the abstract part and the implementation part, thus greatly providing system flexibility. Separate the abstract Part and implementation part and define interfaces separately, which helps to layer the system and generate a better structured system.
● Better scalability
The bridge mode allows the abstract part and the implementation part to expand independently without affecting each other, thus greatly improving the scalability of the system.
________________________________________
Use of Bridge Mode in Java
A typical example of the Bridge Mode in Java applications is the JDBC driver. JDBC provides a common interface for all relational databases. An application system dynamically selects an appropriate drive and sends commands to the database engine through the drive. This process delegates the abstract role behavior to the role.
The abstract role can issue query commands for any database engine, because the abstract role does not directly deal with the database engine, and the JDBC driver is responsible for the underlying work. Because of the existence of JDBC drivers, the application system can evolve independently without relying on the details of the database engine. At the same time, the database engine can also evolve independently from the details of the application system. Shows the two independent hierarchical structures. The left side is the jdbc api hierarchical structure, and the right side is the JDBC driver hierarchical structure. Applications are built on JDBC APIs.
As a hierarchical structure, the application system is relatively independent from the JDBC driver, and there is no static strong association between them. The application system interacts with the JDBC driver through delegation. This is an example of a bridge mode.
The architecture of JDBC separates the abstract part from the specific part, so that the abstract part and the specific part can be expanded independently. For an application, you only need to select different drivers to allow the program to operate on different databases without changing the application, so as to port the program to different databases. For the driver, implementing different drivers for databases does not affect applications.
Author: m13666425773