1. Intention:
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.
2. Scenario Description:
The USB data cable of a mobile phone can be seen as an adapter. Different types of mobile phones are connected to the same USB interface of a computer through different USB data cables. A few years ago, a non-smartphone used a variety of interfaces to connect the data cable to one end of the mobile phone, but the same USB interface was used to connect one end of the computer. Data lines of specific brands allow different mobile phones to be connected to the USB interface of the computer, so that the phone interface can work together with the USB interface of the computer.
In addition, stacks and queues in C ++ are called container adapters. They are all implemented based on linear tables, but they provide specific operation interfaces, for example, the stack LIFO (last-in first-out) feature.
3. Adapter mode class diagram:
Object Adapter class diagram:
Role:
- Client: User class, which uses the new interface target to meet certain requirements.
- Target: a new interface class. It opens a specific Interface request to complete certain operations and works with the client.
- Adaptee: original class.
- Adapter: adapter class, which encapsulates the interfaces in adaptee into new interfaces in target to meet new requirements.
Collaboration:
- The client calls the operations of the adapter instance. The adapter uses adaptee to complete these called operations.
Note:
- The class diagram indicates the two types of chart in the adapter mode.Object AdapterClass diagram, anotherClass AdapterIt is implemented by inheriting two classes or implementing two interfaces.
4. Applicability:
- Use an existing class to meet new requirements, but the original class is inconsistent with the new demand class interface.
- You want to reuse the original class, but some methods do not exist in the original class. You can use the adapter class to modify the interfaces in the original class adaptee and add methods that do not exist in adaptee.
5. instance class diagram:
Description:
- USB port and phoneport are the categories of USB port and mobile phone port.
- Human uses USB dataline to connect the mobile phone to the computer (linkphonetocomputer.
6. code example:
The mobile phone port class that contains the mobile phone port information, corresponding to the adaptee In the adapter mode.
1 package COM. crazysnil. adapter; 2 3 Public class phoneport {4 Public void getphoneport () {5 system. out. println ("I Am a mobile phone interface"); 6} 7}
The USB port class meets the new requirement of connecting the device to the computer, corresponding to the target in the adapter mode.
1 package COM. crazysnil. adapter; 2 3 Public class usbport {4 Public void getusbport () {5 system. out. println ("I Am a USB interface"); 6} 7}
Data Cable type. One end is connected to the mobile phone port, and the USB interface at the other end of the data cable is opened, corresponding to the adapter in adapter mode.
1 package COM. crazysnil. adapter; 2 3 Public class usbdataline extends USB port {4 private phoneport; 5 6 Public usbdataline () {7 system. out. println ("Get a data cable of a mobile phone with a matching feature model"); 8 phoneport = new phoneport (); 9} 10 11 @ override12 public void getusbport () {13 phoneport. getphoneport (); 14 system. out. println ("Connect the mobile phone interface to one end of the data line and enable the USB Interface End of the data line"); 15} 16}
Human, there is a need to connect a mobile phone to a computer, use the data line to connect the phone to a computer, corresponding to the client in the adapter mode.
1 package COM. crazysnil. adapter; 2 3 Public class human {4 public static void linkphonetocomputer (usbdataline line) {5 line. getusbport (); 6 system. out. println ("connecting a USB interface to a computer"); 7} 8}
Test class, effect demonstration.
1 package com.crazysnail.adapter;2 3 public class AdapterTest {4 public static void main(String[] args){5 UsbDataLine newUsbDataLine = new UsbDataLine();6 7 Human.linkPhoneToComputer(newUsbDataLine);8 }9 }
:
7. Features of the adapter Mode
- Class reuse is implemented, and the original adaptee class is used to meet new requirements through packaging or transformation of adapter interfaces.
- When you need to dash the adaptee class, operations in the adapter may be affected.
Structural-adapter Mode