Bridge Mode in Java

Source: Internet
Author: User

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 ();}}


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.