The adapter mode converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Adapter mode structure
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPGgyPrG + xKPKvdKqveK + weight + zdTaw + weight/yhozwvcD4KPHA + PGJyPgo8L3A + weight + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140323/20140323091203140.jpg" alt = "\">
That is, when the data and behavior of the system are correct, but the interface is inconsistent, we should consider using an adapter to match an original object out of the control scope with an interface. The adapter mode is mainly used to reuse some existing classes, but the interfaces are inconsistent with the Reuse Environment.
Note:
1. in use, the adapter mode should not be considered first, but the unified interface should be reconstructed.
2. If the interface is inconsistent, you must be able to control it beforehand. Do not wait until the problem occurs before solving it.
Adapter mode example
Sometimes you need to read data from a USB flash drive when playing with a tablet or mobile phone. However, you know and pad devices use either MicroUSB or miniUSB interfaces, which cannot be directly inserted into the USB flash drive, since the USB flash drive cannot be directly inserted, insert it indirectly. At this time, we only need an OTG line to get it done on the first line.
You can not only insert a USB flash drive on your phone, but also connect the keyboard and mouse to the USB flash drive without any pressure. Hey, you can send text messages on the keyboard...
Next, let's take a look at the OTG line as an adapter, connect to the mobile phone, and then read the code of the U disk data:
Namespace adapter example {class USB // USB interface {public virtual void IOData () // input/output data {Console. WriteLine ("using a USB flash drive to transmit data... ") ;}} Class PhoneMicroUSB // mobile phone charging, data transmission interface {public void transportData () {Console. WriteLine (" data is being transferred using a mobile phone... ") ;}} Class OTG: USB // OTG cable, used to connect the mobile phone and USB disk {private PhoneMicroUSB pmUSB = new PhoneMicroUSB (); public override void IOData () {pmUSB. transportData () ;}} class Program {static void Main (string [] args) {// connect the OTG line to the mobile phone, at this time, the USB flash drive is developed for the USB flash drive interface usb = new OTG (); usb. IOData ();}}}
I really feel that the adapter is a good thing ..