Adapter Mode ★★★★☆

Source: Internet
Author: User
Tags iusb

First, what is the adapter

An adapter is a wrapper class that is transforming an interface into another interface;

Ii. Supplementary Notes

There are two ways of realization, one is through inheritance, the other by combination;

When using the combination implementation, the decorator pattern is a bit similar, but the focus is different, the decorator mode is to add new behavior to the object, adapter is to adapt the interface;

Advantages: In accordance with the open and closed principle, the adaptation of the class and target class decoupling; code reuse;

Third, the role

Target interface

Adapter class

Adapter class

User

Iv. examples, Java implementations

Introduction: Existing A USB interface and a PSP interface implementation class, the function is to charge, the purpose of this example is to the PSP interface implementation Class packaging, so that it can be charged with USB interface;

Target interface , charging with USB interface

Package Com.pichen.dp.structuralpattern;public interface Iusb {public void charge ();

A USB interface implementation class (this class does not belong to the role in adapter mode, just a contrast)

 Package Com.pichen.dp.structuralpattern;  Public class Implements iusb{    @Override    publicvoid  charge () {        System.out.println (" Use HP USB interface to charge ~ ");}            }

Adapter class , charging with PSP

 Package Com.pichen.dp.structuralpattern;  Public class PSP {    publicvoid  Pspcharge () {                System.out.println ("Charge with PSP interface ~");}            } 

Adapter Class ( inherited ), Inherit PSP class, and implement Iusb interface

 Package Com.pichen.dp.structuralpattern;  Public class extends Implements iusb{    @Override    publicvoid  charge () {        super  . Pspcharge ();    }}

Adapter classes (combined ), combining PSP objects, and implementing IUSB interfaces

 Package Com.pichen.dp.structuralpattern;  Public class Implements iusb{    private  psp PSP;          Public PSPAdapter2 (PSP PSP) {        this. PSP= psp;    }    @Override    publicvoid  charge () {        psp.pspcharge ();    }    }

User Main, test results

 Package Com.pichen.dp.structuralpattern;  Public class Main {    publicstaticvoid  main (string[] args) {        new Hpusb ();        Usb.charge ();                 New Pspadapter ();        Usb_psp.charge ();                 New PSPAdapter2 (new  PSP ());        Usb_psp2.charge ();    }}

Printing results:

Charging using the HP USB interface ~ Charging using the PSP interface ~ charging using the PSP interface ~

Adapter Mode ★★★★☆

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.