First, preface
Bridging mode is the separation of the abstract part from its implementation, so that they can all change independently. Abstraction is separated from its implementation, which means that the abstract class and its derived classes are used to implement their own objects. As far as mobile phones are concerned, they can be categorized by brand or by function.
In object-oriented design, we have a very important principle: the synthetic/aggregation reuse principle, which is to prioritize the use of object composition/aggregation rather than class inheritance.
An object's inheritance is set at compile time, so it is no longer possible to change the inheritance from the parent class when it is run. When you need to reuse subclasses, if the inherited implementation is not appropriate to solve the new problem, the parent class must be overridden or replaced by a more appropriate class, which limits flexibility and ultimately limits reusability.
For example, to be able to use a mobile phone, divided into hardware and software parts, they can be combined to become a mobile phone, if we can let the software and hardware (or the model of the mobile phone) of the coupling degree of separation, so long can greatly reduce the face of new demand changes too big unreasonable situation, now can be "mobile brand" and "Mobile software" abstraction, so that different brands and functions to inherit them, so that the addition of new brands or new features will not affect the other classes.
Second, the example code
namespaceBridging Mode {classProgram {Static voidMain (string[] args) {Handsetbrand ab; AB=NewHandsetbrandn (); Ab. Sethandsetsoft (Newhandsetgame ()); Ab. Run (); Ab. Sethandsetsoft (Newhandsetaddresslist ()); Ab. Run (); AB=NewHandsetbrandm (); Ab. Sethandsetsoft (Newhandsetgame ()); Ab. Run (); Ab. Sethandsetsoft (Newhandsetaddresslist ()); Ab. Run (); Console.read (); } } //Mobile Brand Abstract classHandsetbrand {protectedHandsetsoft Soft; //set up your phone software Public voidSethandsetsoft (Handsetsoft soft) { This. Soft =Soft; } //Run Public Abstract voidRun (); } //mobile phone brand n classHandsetbrandn:handsetbrand { Public Override voidRun () {soft. Run (); } } //mobile phone brand M classHandsetbrandm:handsetbrand { Public Override voidRun () {soft. Run (); } } //mobile phone brand s classHandsetbrands:handsetbrand { Public Override voidRun () {soft. Run (); } } //Mobile Software Abstract classHandsetsoft { Public Abstract voidRun (); } //Mobile Games classHandsetgame:handsetsoft { Public Override voidRun () {Console.WriteLine ("Running mobile Games"); } } //Phone Contacts classHandsetaddresslist:handsetsoft { Public Override voidRun () {Console.WriteLine ("run your phone contacts"); } } //Mobile MP3 Playback classHandsetmp3:handsetsoft { Public Override voidRun () {Console.WriteLine ("running mobile MP3 playback"); } }}Iii. Summary
To realize that the system may have multiple angles of classification, each of which is likely to change, then the multi-angle is separated to allow them to change independently, reducing their coupling degree.
C # Object-oriented design--bridging mode (eight)