Java design mode (7)--Adapter mode for structured mode

Source: Internet
Author: User

I. Overview

 Concept

  

  In fact, to give a life example, adapter mode can be analogous to adapters, such as Typec and USB adaptor, the original can only be connected to Typec interface, expand to be able to connect to ordinary USB; Here the adaptor on the one hand need to check on the Typec, on the one hand need as a USB jack.

If you want to cite a Java example, take a look at the JDBC Chapter! A simplified UML diagram for JDBC (the middle of the xxx JDBC is our adapter for JDBC and the major database vendors!) )

  

  Thumbnail

  

   class's Adapter mode

  

The adapter mode of the object (note the relationship of the class, above is the inheritance, the following is the delegation)

  

  

Second, practice

  For examples of the adapters mentioned above, we refer to: http://www.cnblogs.com/V1haoge/p/6479118.html

Of course, this example is essentially the same as the example of the above-mentioned adapter (just the way the interface is separated by function)

  class, we write the characters in the thumbnail

  Target role

/** * Target interface *  Administrator * Date: 2017/10/28 * */publicinterface  Target    {/**      * Target operation 1     *    /void  operation1 ()    ; /**      * Target operation 2     *    /void  operation2 ();}

  SOURCE role

/** * Source Role *  Administrator * Date: 2017/10/28 * */publicclass  adaptee    {public void Operation1 () {        System.out.println("Adaptee#opetion1 ()");}    }

 Adapter

/** * Adapter Role *  Administrator * Date: 2017/10/28 * */publicclassextendsimplements target{    @Override    publicvoid  Operation2 () {        System.out.println ("Adapter#operation2 ()");}    }

  Client uses

 Public class Client {    publicstaticvoid  main (string[] args) {        new  Adapter ();        T.operation2 ();    }}

 The adapter for the object we look at the core of the adapter role

  Adapter

/*** Adapter Role * Administrator * Date: 2017/10/28 **/ Public classAdapterImplementstarget{Privateadaptee adaptee;  PublicAdapter (adaptee adaptee) { This. Adaptee =adaptee; } @Override Public voidOperation1 () {adaptee.operation1 (); } @Override Public voidOperation2 () {System.out.println ("Adapter#operation2 ()"); }}

Java design mode (7)--Adapter mode for structured mode

Related Article

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.