/*** Bridging mode *@authorTmac-j * Applies to multi-dimensional scenarios * in combination with the form of inheritance * in accordance with the principle of single responsibility * A class only one cause of his change * increase program Flexibility*/ Public classBridgepattern {Abstract classroad{car Car; voidrun () {}; } Abstract classcar{voidShowmyname () {}; } Public classStreetextendsroad{ Public voidrun () {car.showmyname (); System.out.println ("Mercedes-Benz on the street."); } } Public classSpeedroadextendsroad{ Public voidrun () {car.showmyname (); System.out.println ("Mercedes-Benz on a high Road"); } } Public classBusextendscar{ Public voidShowmyname () {System.out.println (Bus); } } Public classSedancarextendscar{ Public voidShowmyname () {System.out.println (Car); } } Public voidTest () {Street Street=NewStreet (); Street.car=NewBus (); Street.run ();//The result is a bus running on the street.Speedroad speedroad =Newspeedroad (); Speedroad.car=NewSedancar (); Speedroad.run ();//The result is a sedan on a high-speed road . } }
Bridgepattern (Bridging mode)