Bridging mode: Separates the abstract part from its implementation, allowing them to transform "DP" independently
Overview:
In a software system, some types, because of their own logic, have two or more dimensions that change, so how do you deal with this "multidimensional change"? How can you use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This will use Bridge mode.
Basic code for Bridging mode:
Only one:
Abstractcar:
Package Com.sjmx.bridge; Public Abstract class Abstractcar { void run () {}}
Car:
Package Com.sjmx.bridge; Public class extends Abstractcar { @Override void run () { System.out.print ("Car") ; } }
Bus:
Package Com.sjmx.bridge; Public class extends Abstractcar { @Override void run () { System.out.print ("Bus");} }
Dimension two:
Package Com.sjmx.bridge; Public Abstract class abstractroad { abstractcar aCar; void run () {}; }
Street:
Package Com.sjmx.bridge; Public class extends abstractroad { @Override void run () { acar.run (); System.out.println ("driving in urban streets");} }
Speedway:
Package Com.sjmx.bridge; Public class extends abstractroad { @Override void run () { acar.run (); System.out.println ("driving on the freeway");} }
Client:
Package Com.sjmx.bridge; Public class Client { publicstaticvoid main (string[] args) { new Speedway (); New Car (); Speedway.run (); New Street (); New Bus (); Street.run (); } }
Operation Result:
The car is on the freeway and the bus runs in the city streets
Below, if we continue to expand our business, the great benefits of bridging mode will be revealed:
People:
Package Com.sjmx.bridge; Public Abstract class people { abstractroad road; void run () {} }
Woman:
Package Com.sjmx.bridge; Public class extends people { @Override void run () { System.out.print ("Woman Open"); Road.run (); }}
Mans:
Package Com.sjmx.bridge; Public class extends people { @Override void run () { System.out.print ("man Open"); Road.run (); }}
Client 2:
Package Com.sjmx.bridge; Public class Client2 { publicstaticvoid main (string[] args) { new Speedway (); New Car (); New Man (); = Speedway; Man.run (); }}
A man drives a car on the freeway
With the above code, have you found the great benefits of using bridging mode, using bridging mode each abstract class holds excuses or abstract classes, greatly reducing the coupling between the different dimensions. And the use of bridging mode is extremely easy to extend, which is completely unmatched by inheritance
effect and key points of realization:
1. Bridge mode uses a "composite relationship between objects" to decouple the inherent binding between abstractions and implementations, allowing abstractions and implementations to evolve along their respective dimensions.
2. The so-called abstraction and implementation along the respective dimensions of the change, namely "sub-class" them, to obtain the sub-classes, they can be arbitrary, so as to obtain different vehicles on different roads.
3. Bridge mode is sometimes similar to multi-inheritance scenarios, but multiple inheritance schemes often violate the single-class principle of responsibility (that is, one class has only one change), and reusability is poor. Bridge mode is a better solution than a multi-inheritance scheme.
4. Bridge mode is generally used in "two very strong change dimensions", sometimes even with two changing dimensions, but the dimension of change in one direction is not dramatic-in other words, two changes do not result in criss-crossing results, and bridge mode is not necessarily used.
Applicability:
The bridge mode should be used in the following cases:
1. If a system needs to add more flexibility between the abstract and materialized roles of the artifacts, avoid establishing a static connection between the two levels.
2. Design requires that any change to the implementation role should not affect the client, or that the change to the implementation role is completely transparent to the client.
3. A component has more than one abstract role and an implementation role, and the system requires dynamic coupling between them.
4. While there is no problem with using inheritance in the system, the design requirements need to be managed independently because the abstract and materialized roles need to change independently.
Summarize:
Bridge mode is a very useful pattern and very complex, it is well suited to the open-closed principle and priority to use the object, rather than inherit these two object-oriented principles.
The difference between bridging mode and decoration:
Decoration Mode:
These two patterns are to some extent to reduce the number of subclasses and avoid complex inheritance relationships. However, the methods they solve are different, the decorative mode of the class in the more than the base class in a separate class, to adapt to the need for new functions, when we describe the new function of the class encapsulated in the base class object, we get the necessary subclass object, These classes, which describe new features, can be combined to achieve many combinations of functionality.
Bridging mode:
Bridging mode abstracts the implementation details of the original base class, constructs it into an implementation structure, and then changes the original base class into an abstract hierarchy so that the system can change independently on multiple dimensions.
"Structural mode", "Big Talk design mode"--feeling book (13) when can mobile phone software be unified? --Bridging mode