Design mode adapter mode (Adapter)

Mode of action is to convert the adapter interface of a class clients expect another interface, such as those based otherwise because of incompatible interfaces can not work together with the work. Practice is like its own interface wrapped in an existing class. This type of design pattern belongs structural design patterns .

UML role:

Source : needs to be adapted classes, interfaces, objects, namely Datas. 
Where do you want : the need to get the class, Source adapting get through the class object, that is, we look forward to an excuse to get. 
Adapter : adapter class, coordination Source and Destination, so that the two can work together.

Method to realize:

Class Adapter mode (implemented using inheritance)
Object Adapter (combination implemented using object)
interface adapter mode

Scenario:

The system requires the use of an existing class, but is not compatible with existing classes.
You need to create a reusable class for some of the little relationship to each other class, and easy to expand, in order to face the future will be like.
We need a unified output interface, but the input type was unpredictable.

 

Class Adapter Mode Example Analysis

Usage scenarios: when we want to access port A is not the way we wanted, but found in another interface B in the appropriate way, we do not change the access interface A, in this case, we can define an adapter method C to transfer, the adapter implements the interface a to C we visit, so that we can continue the current access method in the interface a, then B interface inheritance implementation class B1, B and we can access the interface adapter is C up, direct reference to a suitable time in method B1 a method we interfaces in the adapter C, thus completing a simple adapter class.

To divert ps2 and usb example

ps2 Interface: Ps2

1 public interface Ps2 {
2     void isPs2();
3 }

USB Interface: Usb

1 public interface Usb {
2     void isUsb();
3 }

USB interface category: Usber

public class Usber implements Usb {
     @Override
    public void isUsb() {
         System.out.println("USB口");
     }
 
 } 

Adapters: Adapter

public class Adapter extends Usber implements Ps2 {

     @Override
     public void isPs2() {
         isUsb();
     }
 
 }

Test categories: Clienter

1 public class Clienter {
2 
3     public static void main(String[] args) {
4         Ps2 p = new Adapter(new Usber());
5         p.isPs2();
6     }
7 
8 }

The results show:

USB port

Examples to explain:

  I have a ps2 plug the device, but only on the host adapter plug usb interface, so to find a converter to convert ps2 plug USB plug can become allow the host access to the device ps2.

  Interface Ps2: Description Interface Format ps2

  Interface Usb: USB Interface Format Description

  Class Usber: Usb is the interface implementation class, it is a specific USB Interface Format

  Adapter: ps2 interface format for converting to become a USB interface format

 

Reference link: https: //www.cnblogs.com/V1haoge/p/6479118.html

 

Guess you like

Origin www.cnblogs.com/lucky1024/p/11549578.html