Overview
In software systems, some types of software systems have two or more dimensional changes due to their own logic. How can we deal with such "multi-dimensional changes "? How can we use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This requires the Bridge mode.
Intention
Separate the abstract part from the implementation part so that they can all change independently. [GOF design patterns]
Structure chart
Figure 1 Bridge Mode Structure
Examples in life
The Bridge Mode separates the abstract part from its implementation so that they can change independently. A common switch-controlled electric lamp or fan is an example of bridging. The purpose of the switch is to turn the device on or off. The actual switch can be a simple double-knife zipper switch or a dimming switch.
Figure 2 bridging object graph using an electronic switch example
Explanation of Bridge Mode
In the creation mode, I have mentioned abstraction and implementation. abstraction should not depend on specific implementation details. Implementation Details should depend on abstraction. Take a look at the figure below:
Figure 3 abstraction should not depend on Implementation Details
In this case, if abstract B is stable and details B is changed, then the creation mode is used to solve the problem. But if abstract B is also unstable and changing, what should we do? This requires the Bridge mode.
We still use the logging tool to illustrate the Bridge mode. Now we need to develop a general logging tool that supports two methods: database recording DatabaseLog and text file recording FileLog. At the same time, it can run in. and can also run on the Java platform.
Based on our design experience, we should treat different logging methods as separate objects and abstract a base-class Log for the logging class, different logging methods inherit from this base class:
Figure 4 Log structure
The implementation code is as follows:
In addition, considering the log records of different platforms, the methods used to operate databases and write text files may be different. For this reason, for different logging methods, we need to provide implementation on various platforms and further design the above classes to obtain the following structure:
Figure 5
The implementation code is as follows:
There is no error in the current design scheme. If we want to introduce a new record method for xml files, the above class structure will become:
Figure 6
As shown in the blue section, we have added a new subclass inherited from the Log base class without modifying other subclasses. This also conforms to the open-closed principle. If we introduce a new platform, for example, the logging tool we have developed also needs to support the Borland platform, then the structure of this type is changed:
Figure 7
Similarly, we didn't modify anything, but added two subclasses inherited from DatabaseLog and TextFileLog, which also conforms to the open-closed principle.
However, we can say that this design is fragile. After careful analysis, we can find that there are still many problems with it. First, it follows the principle of openness and closure while, it violates the single responsibility principle of a class, that is, a class has only one reason for its change, but there are two reasons for the change of the Log class, that is, the log record mode changes and the log record platform changes. Second, there will be a lot of repeated code, and some code will be the same for different log record methods on different platforms; once again, the structure of the class is too complex, there are too many inheritance relationships, and it is difficult to maintain. The most fatal point is that the scalability is too poor. The changes we analyzed above are only in a certain direction. If the changes change along the log record method and different operating platforms, we will see that the structure of this class will rapidly become large.
Now it's time to make the Bridge model debut. We need to decouple the changes in these two directions and change the strong coupling relationship between them into weak connections. We treat the logging method and the implementation on different platforms as two independent parts. For the logging method, the class structure diagram is still as follows:
Figure 8
Now we introduce another abstract class ImpLog, which is the basic class for logging implementation on different platforms. The structure diagram is as follows:
Figure 9
The implementation code is as follows:
public abstract class ImpLog { public abstract void execute(String msg);}public class JImpLog extends ImpLog {public void execute(String msg) {System.out.println(msg);}}public class NImpLog extends ImpLog {public void execute(String msg) {System.out.println(msg);}}
At this time, the log record method and the two classes on different operating platforms can be changed independently. The job we need to do is to connect these two classes. So how to connect? Here, Bridge uses the object combination method. The class structure is as follows:
Figure 10
The implementation code is as follows:
public abstract class Log {public ImpLog implementor; public void write(String log) { implementor.execute(log); }}public class DatabaseLog extends Log {public void write(String log) {implementor.execute(log);}}public class TextFileLog extends Log{public void write(String log) {implementor.execute(log);}}
As you can see, through the combination of objects, the Bridge mode changes the inheritance relationship between two roles to a coupling relationship, so that the two can be independently changed, this is also the intention of the Bridge model. Let's take a look at how to use the client:
Public class Client {public static void main (String [] args) {Log dblog = new DatabaseLog (); dblog. implementor = new NImpLog (); dblog. write ("Database Log On the NET Platform"); Log txtlog = new TextFileLog (); txtlog. implementor = new JImpLog (); txtlog. write ("Text File Log on Java platform ");}}
Results and key points of implementation
1. The Bridge Mode decouples the inherent binding relationship between abstraction and implementation using the "Composite relationship between objects", so that abstraction and implementation can change along their respective dimensions.
2. The so-called abstraction and implementation are changed along their respective dimensions, that is, they are "subclass". After each subclass is obtained, they can be arbitrary and different models on different platforms can be obtained.
3. The Bridge mode is sometimes similar to a multi-inheritance scheme, but a multi-inheritance scheme often violates the single responsibility principle of the class (that is, a class has only one reason for change), and the reusability is poor. The Bridge mode is a better solution than multi-inheritance solutions.
4. the Bridge mode is generally used in "two very strong changing dimensions". Sometimes, even if there are two changing dimensions, however, the changing dimension in a certain direction is not drastic-in other words, the two changes do not result in cross-border results and do not have to use the Bridge mode.
Applicability
The bridge mode should be used in the following situations:
1. If a system needs to increase more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.
2. The design requires that any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
3. A component has more than one abstract role and actual role, and the system needs dynamic coupling between them.
4. Although there is no problem in using inheritance in the system, abstract roles and specific roles need to be changed independently, and design requirements need to be managed independently.
Summary
The Bridge mode is a very useful and complex mode. It is well compliant with the open-closed principle and the preferential use of objects, rather than inheriting these two object-oriented principles.
Selected from
. NET design mode (9): Bridge Pattern)