Bridge pattern-Bridge Mode

Source: Internet
Author: User

BridgeThe mode is also calledHandle/bodyMode.

In software systems"Some objects with Complex Structures"Because of the changes in requirements, these objects often face drastic changes, but they have stable and consistent interfaces. Most of the Creation modes are designed to address"CustomerProgram"Isolated"These variable objects"So that"Customer programs dependent on these variable objects"Does not change as demand changes.

Instance analysis The braking distance of the Mercedes-Benz car (the distance needed from this speed to the distance needed to completely stop) varies with the friction of the tires. Michelin tires are used now
 1   Class Michelin  2   {  3       Public  :  4 Michelle inwheel ( Int  Q ,...);  5           Int  Getload ();  6       Private  :  7           Int Qulity;  8   ....  9   };  10 Michelle inwheel: Maid ( Int  Q ,....)  11   : Quilty (q)  12   {}  13   Int  Michelle inwheel: getload ()  14  {  15       //  The load capacity of the tire is obtained through some factors and formulas.  16   ...  17       Return  Load;  18   }  19   20   Class  Benzcar  21   {  22      Public  :  23           Int  Getdistance ()  24       Private  :  25 Michelin * Wheel  26           Int  Speed  27   };  28 Benzcar: benzcar (michainwheel *W)  29   {  30 Wheel = W;  31   }  32   Benzcar: getdistance ()  33   {  34 Carspeed = Speed;  35       //  The braking distance at a certain speed is obtained through some factors and formulas. 36   ...  37       Return  Distance  38 }

So here we can see that benzcar is very dependent on this class of Michelin inwheel, because the obtained braking distance is mainly caused by the tire load force parameter, So we generally design the class, it can be abstracted and generalized, if a car uses a Michelin wheel, its braking distance is the same as above.

 1   Class  Michelin  2   {  3       Public  : 4 Michelle inwheel ( Int  Q ,...);  5           Int  Getfriction ();  6       Private  :  7           Int  Qulity;  8   ....  9   };  10 Michelle inwheel: Maid ( Int  Q ,....)  11   : Quilty (q)  12   {}  13   Int  Michelle inwheel: getfriction ()  14   {  15       //  The friction of the tire is obtained through some factors and formulas.  16   ... 17       Return  Friction;  18   }  19   20   Class  Car  21   {  22 Vritual Int Getdistance () = 0  ;  23   }; 24   25   Class Benzcar: Public  Car  26   {  27       Public  :  28           Int  Getdistance ()  29       Private  :  30 Michelin *Wheel  31           Int  Speed  32   };  33 Benzcar: benzcar (michainwheel * W)  34   {  35 Wheel = W;  36   }  37   Benzcar: getdistance () 38   {  39 Carspeed = Speed;  40       //  The braking distance at a certain speed is obtained through some factors and formulas.  41   ...  42       Return  Distance  43 }

Now it seems that the abstract class of automobile is dependent on the class "Michelin wheel", and its braking distance is affected by the class "Michelin wheel. Now the actual situation is that cars not only use Michelin tires, but also Bridgestone tires and others. If Bridgestone tires are used, the friction calculation is changed. Therefore, Abstract: The braking distance only depends on the Friction obtained from the abstract tires.

 1   Class  Wheel  2   {  3       Public  :  4           Virtual   Int Getfriction () = 0  ; 5   };  6   7   Class Michelle inwheel: PublicWheel  8   {  9       Public  :  10 Michelle inwheel ( Int  Q ,...);  11           Int Getfriction ();  12       Private  :  13           Int  Qulity;  14   ....  15   };  16 Michelle inwheel: Maid ( Int  Q ,....)  17   : Quilty (q)  18  {}  19   Int  Michelle inwheel: getfriction ()  20   {  21       //  The friction of the tire is obtained through some factors and formulas.  22   ...  23       Return  Friction;  24   }  25  26   Class  Car  27   {  28 Vritual Int Getdistance () = 0  ;  29   };  30   31   Class Benzcar: Public  Car  32  {  33       Public  :  34           Int  Getdistance ()  35       Private  :  36 Wheel * Wheel  37           Int  Speed  38   }; 39 Benzcar: benzcar (wheel * W)  40   {  41 Wheel = W;  42   }  43   Benzcar: getdistance ()  44   {  45 Carspeed = Speed;  46       // The braking distance at a certain speed is obtained through some factors and formulas.  47   ...  48       Return  Distance  49 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.