Bridge Mode in Java
/*** Mobile phone case, set * @ author stone **/public abstract class BaseCellphoneShell {public abstract void mapping (); // mobile phone number matching}
/*** Mobile phone, bridging the mobile shell BaseCellphoneShell * @ author stone */public abstract class BaseCellphone {private BaseCellphoneShell shell; public void setShell (BaseCellphoneShell shell) {this. shell = shell;} public BaseCellphoneShell getShell () {return shell;} public abstract void mapping (); // shell}
/*** Mobile implementation ** @ author stone **/public class Cellphone extends BaseCellphone {@ Overridepublic void mapping () {System. out. println ("the shell of this phone is:"); getShell (). mapping ();}}
/*** Mobile shell implementation ** @ author stone **/public class CellphoneShell extends BaseCellphoneShell {@ Overridepublic void mapping () {System. out. println ("shell ---- iphone6 ");}}
/** Bridge Mode: the structural mode ** decouples abstraction and implementation so that the two can change independently, that is, to change the strong associations between them into weak associations, * This means that the combination/aggregation relationship is used between the abstraction and implementation of a software system, rather than the inheritance relationship, so that the two can change independently. */Public class Test {public static void main (String [] args) {/** in this example: there is a strong association between the mobile phone and the mobile phone shell: either the mobile phone corresponds to a shell (BaseCellphoneShell) or the shell corresponds to a mobile phone (BaseCellphone) * The mobile phone can be changed in multiple dimensions, for example, mobile phone cases are divided by brand, size, and individual needs. * mobile phone cases also have multidimensional changes, because it needs to adapt to the previously divided mobile phone *, the bridge is implemented in BaseCellphone, and the aggregation is connected to BaseCellphoneShell */BaseCellphone cellphone = new Cellphone (); cellphone. setShell (new CellphoneShell (); // aggregate access cellphone. mapping ();}}