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