Build mode--adapter mode (JAVA)

Source: Internet
Author: User

Adapter mode:

Adaptor is equivalent to our adapter, such as mobile phone charger plug (Xiaomi and Huawei's mobile phone charger can not be shared, this time you can give Huawei Charger Press a adapter, you can charge Xiaomi mobile phone).

Similarly, when a class (charger Huaweicharger) implements an interface (Huawei Huaweiphone), but sometimes we need to implement another interface (Millet Xiaomiphone), if the charger to implement the Xiaomi interface, you need to implement the method in the Mi interface , if the Charger class has been hit in a package, this time the modification is more difficult, also violate the open and close principle (expansion, modification closed). So we need to build an adapter class (Htxadapter Huawei Transform Xiaomi Adapter) to implement the Xiaomi interface, while inheriting from the Charger class.

1 /**2 * Huawei Mobile Phone Interface3  * 4  * @authorDennyzhao5 * @date June 20, 20176  * @version1.07  */8  Public InterfaceHuaweiphone {9     Ten      Public intGetsquareplug (); One      A      Public intgetsquareinterface (); -      -      Public voidGetandroidsys (); the      -      Public voidgethuaweiinput (); -      -      Public voidgetintercpu (); +      -      Public voidGetinterboard (); +  A}
Huaweiphone
1 /**2 * Xiaomi Phone3  * 4  * @authorDennyzhao5 * @date June 20, 20176  * @version1.07  */8  Public InterfaceXiaomiphone {9     Ten      Public intGetcircleplug (); One      A      Public intgetcircleinterface (); -      -      Public voidGetandroidplussys (); the      -      Public voidgetxiaomiinput (); -      -      Public voidgetamdcpu (); +      -      Public voidGetamdboard (); +}
Xiaomiphone
1 /**2 * Huawei Charger3  * 4  * @authorDennyzhao5 * @date June 20, 20176  * @version1.07  */8  Public classHuaweichargerextendsHuaweiphoneimplImplementshuaweiphone{9 Ten @Override One      Public intGetsquareplug () { ASystem.out.println ("Squareplug----------"); -         return0; -     } the  - @Override -      Public intGetsquareinterface () { -System.out.println ("Squareinterface----------"); +         return0; -     } +  A  at}
Huaweicharger
1 /**2 * Description: The design principle stresses the expansion open, the modification closes. So when we need to implement another interface for a class, we can implement the interface by creating subclasses. 3  * 4  * @authorDennyzhao5 * @date June 12, 20176  * @version1.07  */8  Public classHtxadapterextendsHuaweichargerImplementsxiaomiphone{9 Ten     /** One * Millet Adaptation A      */ - @Override -      Public intGetcircleplug () { the         intA =Super. Getsquareplug (); -         returnA + 5; -     } -  +     /** - * Millet Adaptation +      */ A @Override at      Public intGetcircleinterface () { -         intb =Super. Getsquareinterface (); -         returnB + 4; -     } - @Override -      Public voidGetandroidplussys () { in         //TODO auto-generated Method Stub -          to     } +  - @Override the      Public voidGetxiaomiinput () { *         //TODO auto-generated Method Stub $         Panax Notoginseng     } -  the @Override +      Public voidgetamdcpu () { A         //TODO auto-generated Method Stub the          +     } -  $ @Override $      Public voidGetamdboard () { -         //TODO auto-generated Method Stub -          the     } - Wuyi}
Htxadapter

The above code will tell that Htxadaper implements all the methods of the Xiaomi interface so that it does not pollute the huaweicharger. No need to be removed at any time. Plug-and-pull construction.

Build mode--adapter mode (JAVA)

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.