Overview
Bridge mode is to solve the separation of the abstract and implementation parts so that they all have multidimensional changes in their own dimensions. This sentence is good understanding, but I after learning bridge mode, there are some doubts, fortunately now figured out. Now I'm going to give you the idea of bridge mode and my questions and hope it's good for you.
Copyright notice
Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
This article Coding-naga
Published: March 31, 2016
This article link: http://blog.csdn.net/lemon_tree12138/article/details/51024127
Source: CSDN
More information: Classification >> Design Patterns
Scene
Now assume that there are many different models of television sets, and a number of different remote control production manufacturers. Remote control manufacturers to make the production of these televisions remote control, but these TV models too many, some TV models are too miscellaneous, even in the late may appear some different models of television.
Common aggregation Scheme
In this general scenario, the remote controller produces a remote control for each television set. The class diagram is as follows
Each of the specific remote control to inherit or implement the remote control abstraction or interface, and in each specific remote control of the internal aggregation of the respective TV interface. This solution solves the problem and can solve the relationship between the remote control and the TV set. However, there is a problem, as mentioned in the above scenario, if there are some new TV sets, then the production of remote control manufacturers have to produce a new remote control. In this way the number of remote control is not only difficult to grasp, and such a complex design.
Bridge mode
The above general aggregation scheme does not solve the general of the remote control, the new model can not be compatible and adapt. We have to redesign the code structure for a situation where there are multidimensional extensions to the abstraction or implementation. Here is the bridge mode, and how the bridge mode solves this problem.
Defined
Separate the abstract part (abstraction) from the implementation part (Implementor) so that they can vary independently.
Analysis
The application scenario for Bridge mode is an abstraction or implementation that may have multidimensional changes. Like the remote control example above, we may have different producers in the remote control abstraction, which may be RCA, Sony and so on in the realization of the TV. Bridge mode allows the abstraction of the remote control and the implementation of the TV to vary in the direction of each other, without worrying about other things.
In the above I also analyzed the general polymerization scheme is not reliable, it can solve some problems, but it is cumbersome, but also unnecessary.
Here is another example that everyone might be familiar with. such as the Java program we are writing now, and the relationship between the running platform. We can have a lot of Java programs, and the running platform can be Windows, Linux or Mac. Our Java programs need to be executed on all platforms, and every platform is guaranteed to be full Java programs.
Class diagram
Here we get the abstract class of the remote control to aggregate the TV's interface. Because so the television has realized the television this interface, then the remote control's abstract class also aggregates all television sets realization. This way, no matter how your TV model changes, the remote control can continue to work without modification.
Note: I have a little question here, why our TV interface is not aggregated into the sub-class of the remote control, but rather aggregated into the remote control of the abstract class it? In other words, do I need to have a remote control abstract class here?
Now I assume that there is no such Remotecontrol abstract class, then Concreteremote will hold the TV interface, but this does not extend the dimension of the remote control. The point of the Bull X in bridge mode is that it allows multiple degrees of abstraction and implementation to be arbitrarily extended on multiple dimensions.
Logical implementation
Based on the class diagram above, you can write Java code.
Tv.java
publicinterface TV { publicvoidon(); publicvoidoff(); publicvoidtuneChannel(int channel);}
Rca.java
Public class RCA implements TV { @Override Public void on() {System.out.println ("RCA was opened."); }@Override Public void off() {System.out.println ("RCA is off."); }@Override Public void Tunechannel(intChannel) {SYSTEM.OUT.PRINTLN ("Switch to section"+ Channel +"the channel."); }}
Sony.java
Public class Sony implements TV { @Override Public void on() {System.out.println ("Sony was opened."); }@Override Public void if() {System.out.println ("Sony was opened."); }@Override Public void Tunechannel(intChannel) {SYSTEM.OUT.PRINTLN ("Switch to section"+ Channel +"the channel."); }}
Remotecontrol.java
Public Abstract class remotecontrol { protectedTV TV =NULL; Public Remotecontrol(TV _TV) { This. TV = _TV; } Public Abstract void on(); Public Abstract void off(); Public void Setchannel(intChannel) {if(TV! =NULL) {Tv.tunechannel (channel); } }}
Concreteremote.java
Public class concreteremote extends remotecontrol { Private intCurrentstation =0;Private Final intMax_station = -; Public Concreteremote(TV _TV) {Super(_TV); }@Override Public void on() {Tv.on (); }@Override Public void off() {Tv.off (); } Public void Setchannel(intChannel) {currentstation = channel;Super. Setchannel (Currentstation); } Public void Nextchannel() {currentstation = (max_station + currentstation +1)% Max_station;Super. Setchannel (Currentstation); } Public void Previouschannel() {currentstation = (max_station + currentstation-1)% Max_station;Super. Setchannel (Currentstation); }}
Model evaluation
| Advantages |
Disadvantages |
| The implementation of the TV is decoupled, and it is no longer permanently bound to the remote |
Relatively complex |
| Remote control and TV can be expanded independently without affecting the other |
|
Ref
GitHub Source Link
Https://github.com/William-Hai/DesignPattern-Bridge2
Java design mode--bridge mode