Adapter Mode Summary

Source: Internet
Author: User

Adapter Mode Core idea: The object of the A interface is transformed into an object of the B interface, so that it looks like a B object. But it is true that the A object is running the actual business logic.

The first feeling of seeing the adapter pattern is this: Is this a pattern? That's how it should be handled!

Life has a lot of live examples, such as network adapter (NIC), modem, data cable, charger and so on.

The downside is. There needs to be such an extra talent for conversion.

Advantages of the adapter mode: Good scalability and class reusability are achieved.

A simple example is given below. Transfer the PS/2 keyboard to a USB port.

Package adapterpattern;/** * PS Keyboard Interface */public interface PS2 {String A (); String B (); String C ();


Package adapterpattern;/** * USB Keyboard Interface */public interface USB {String A (); String B (); String C ();

Package adapterpattern;/** * PS2 Keyboard */public class Ps2keyboard implements ps2{@Overridepublic String A () {return ' [email PR Otected] ";} @Overridepublic String B () {return ' [email protected] ';} @Overridepublic String C () {return ' [email protected] ';}}

Package adapterpattern;/** * PS2 to USB Converter */public class Adapter implements Usb{private Ps2keyboard keyboard;public Adapter (Ps2keyboard keyboard) {this.keyboard = keyboard;} @Overridepublic String A () {return keyboard. A ();} @Overridepublic String B () {return keyboard. B ();} @Overridepublic String C () {return keyboard. C ();}}

Package Adapterpattern;public class Test {public static void main (string[] args) {USB Usbkeyboard = new Adapter (New Ps2key Board ()); System.out.println (USBKEYBOARD.A ()); System.out.println (USBKEYBOARD.B ()); System.out.println (USBKEYBOARD.C ());}}


Test results

[Email protected] [Email protected] [Email protected]


Adapter Mode Summary

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.