Design Mode---bridging mode

Source: Internet
Author: User

When a thing or process has a lot of changes, such as walking, how we use inheritance, the number of classes will grow geometrically.

But how do we isolate each change, that is, the car is isolated from the road, so that each change is independent.

 Public Abstract class Automobile {    publicstaticfinalint type_car = 1;      Public Static Final int Type_motorcycle = 2;          Public Abstract int Run ();}

For the road, it affects the different ratios.

 PackageCom.jayfulmath.designpattern.bridge; Public Abstract classRoad { Public Static Final inttype_city = 1;  Public Static Final intType_highway = 2;  Public Static Final intType_suburban = 3; protectedString TAGNAME; protectedAutomobile _mauto; protected Double_miles;  Public Abstract Doublespeedrate ();//car speed ratio PublicRoad (Double_miles) {         This. _miles =_miles; }    /**     * @param_mauto the _mauto to set*/     Public voidSet_mauto (Automobile _mauto) { This. _mauto =_mauto; }     Public DoubleRun () {//System.out.println ("distance of" +tagname+ "is" +_miles+ "Miles");System.out.println (String.Format ("distance of%s is%.0f miles", Tagname,_miles)); DoubleSpeed = () *speedrate (); DoubleH = _miles/Speed ; System.out.println (String.Format ("%s spend time%.2f hours", tagname,h)); returnh; }}


 PackageCom.jayfulmath.designpattern.bridge; Public classCarextendsAutomobile {Private StaticCar _minstance =NULL; PrivateCar () {Super(); }         Public StaticCar getinstance () {if(_minstance = =NULL) {_minstance=NewCar (); }        return_minstance; } @Override Public intRun () {//TODO auto-generated Method StubSystem.out.print ("Car Run 50km/h at:"); return50; }}


 PackageCom.jayfulmath.designpattern.bridge; Public classMotorcycleextendsAutomobile {Private StaticMotorcycle _minstance =NULL; Privatemotorcycle () {Super(); }         Public StaticMotorcycle getinstance () {if(_minstance = =NULL) {_minstance=Newmotorcycle (); }        return_minstance; } @Override Public intRun () {//TODO auto-generated Method StubSystem.out.print ("Motorcycle Run 30km/h at:"); return30; }}


 PackageCom.jayfulmath.designpattern.bridge; Public classHighWayextendsRoad { PublicHighWay (Double_miles) {        Super(_miles);  This. TAGNAME = "HighWay"; } @Override Public Doublespeedrate () {//TODO auto-generated Method StubSystem.out.println ("in" +TAGNAME); return1.5; }}
 PackageCom.jayfulmath.designpattern.bridge; Public classSuburbanextendsRoad { PublicSuburban (Double_miles) {        Super(_miles);  This. TAGNAME = "Suburban"; } @Override Public Doublespeedrate () {//TODO auto-generated Method StubSystem.out.println ("in" +TAGNAME); return1.0; }}
 PackageCom.jayfulmath.designpattern.bridge; Public classCityroadextendsRoad { PublicCityroad (Double_miles) {        Super(_miles);  This. TAGNAME = "Cityroad"; } @Override Public Doublespeedrate () {//TODO auto-generated Method StubSystem.out.println ("in" +TAGNAME); return0.75; }}

Automotive in simple Factory mode package:

 PackageCom.jayfulmath.designpattern.bridge; Public classAutomobilefactory { Public StaticAutomobile Createautomobile (intCartype) {Automobile _mauto=NULL; Switch(cartype) { CaseAutomobile.type_car: _mauto=car.getinstance ();  Break;  Caseautomobile.type_motorcycle: _mauto=motorcycle.getinstance ();  Break; }        return_mauto; }}
 PackageCom.jayfulmath.designpattern.bridge;Importcom.jayfulmath.designpattern.main.BasicExample;/*Bridge mode: When things change with a lot of convenience, we can make each change separate * for different aspects, you can do the corresponding **/ Public classBridgemainextendsbasicexample {@Override Public voidStartdemo () {/*1.highway 50km car *2.cityway 30km motorcycle *3.suburban 10km car **/            DoubleH1 = GetRuntime (road.type_highway,automobile.type_car,50); DoubleH2 = GetRuntime (road.type_city,automobile.type_motorcycle,30); DoubleH3 = GetRuntime (road.type_suburban,automobile.type_car,10); System.out.println ("Total time is" + (h1+h2+H3)); }        Private DoubleGetRuntime (intRoadtype,intCartype,intpathlength) {        DoubleHour = 0; Road _mroad=roadfactory.createroad (Roadtype, pathlength);        _mroad.set_mauto (Automobilefactory.createautomobile (Cartype)); (); System.out.println ("***************************************"); returnhour; }}


Distance of HighWay is kmcar Run 50km/0.67 hours***************************************   kmmotorcycle Run 30km/1.33 hours***************************************   kmcar Run 50km/0.20 hours***************************************  2.2

The system has multi-angle classification, each angle is likely to change, separate them, not the way of inheritance, but in the form of aggregation, can avoid the explosion of classes.

Design Mode---bridging mode

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: 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.