Home  >  Article  >  Java  >  Detailed explanation of graphic code about adapter pattern in Java

Detailed explanation of graphic code about adapter pattern in Java

黄舟
黄舟Original
2017-07-24 15:29:051734browse

This article mainly introduces the Java adapter mode. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.

1. Concept

The adapter pattern converts one interface into another that the customer wants interface. It allows classes to work together that originally could not work together due to incompatible interfaces.

2. UML

3. More vivid Example

#4. Example analysis

I bought a notebook last year and paired it with a Logitech G1 optical sleeve. What's really annoying is that the mouse of the photoelectric case has a USB interface and the keyboard has a PS2 interface, but my laptop doesn't have a PS2 interface. So I went to the market and bought an adapter.

So, I abstracted several classes.

1.PS2Port (PS2 interface).


2.USBPort (USB interface).


3.PS2ToUSB (object adapter), replace the PS2 interface with a USB interface.


4.TestAdapter (test class), client.


PS2Port



package com.bjpowernode.adapter; 
 
/** 
 * PS2接口,圆口 
 * @author eason 
 * 
 */ 
public interface PS2Port { 
  public void workWithPS2(); 
}

USBPort


##

package com.bjpowernode.adapter; 
 
/** 
 * USB接口,U口 
 * @author eason 
 * 
 */ 
public interface USBPort { 
  public void workWithUSB(); 
}

PS2ToUSB


package com.bjpowernode.adapter; 
 
/** 
 * 对象适配器 
 * 将PS2接口装换成USB接口 
 * 所以此类类型是USB接口(implements USBPort) + 成员变量ps2Port 
 * @author eason 
 * 
 */ 
public class PS2ToUSB implements USBPort{ 
 
  private PS2Port ps2Port; 
   
  public PS2ToUSB(PS2Port ps2Port) { 
    this.ps2Port = ps2Port; 
  } 
   
  @Override 
  public void workWithUSB() { 
    System.out.println("转换的关键在这里,本来是"); 
    ps2Port.workWithPS2(); 
    System.out.println("经过你的转换,现在是USB工作中"); 
  } 
   
}

TestAdapter


##
package com.bjpowernode.adapter; 
 
/** 
 * 测试类 
 * client 
 * @author eason 
 * 
 */ 
public class TestAdapter { 
 
  public static void main(String[] args) { 
    //我现在有一个PS2接口 
    PS2Port ps2Port = new PS2Port() { 
      @Override 
      public void workWithPS2() { 
        System.out.println("PS2工作中"); 
      } 
    }; 
     
    //但是我需要的是一个USB接口啊,对我(client)来说,我只认识USB接口 
    //经过PS2ToUSB的转换,PS2接口变成了USB接口 
    USBPort ps2ToUsbPort = new PS2ToUSB(ps2Port); 
     
    ps2ToUsbPort.workWithUSB(); 
     
  } 
 
}


5. Object Adapter And class adapter

#The above adapter is the object adapter. Let’s look at class adapters again.

PS2ToUSB, just simulate it briefly. Because Java does not allow multiple inheritance, there is no class pattern code in Java, only ideas.


package com.bjpowernode.adapter; 
 
/** 
 * 类适配器 
 * @author eason 
 * 
 */ 
public class PS2ToUSB implements USBPort, PS2Port{ 
 
  //重写workWithUSB,把工作交给workWithPS2 
  @Override 
  public void workWithUSB() { 
    workWithPS2(); 
  } 
   
}

The difference is: the object adapter implements the interface (USB) that the client wants, and has a reference to the adapted object (PS2) internally. Adaptation function is achieved through combination. The class adapter implements the interface (USB) desired by the client and the adapted object interface (PS2), and implements the adaptation function through inheritance.


6. Usage scenarios and usage experience

1. I hope to reuse some existing classes, but the interface is It is inconsistent with the requirements of the reuse environment.

2. In fact, the adapter mode is a bit helpless. In the early design, we should not consider the adapter mode, but should consider refactoring the unified interface.


7. Adapter pattern and decorator pattern


They can both be used to wrap objects, the essential difference is

1. Adapter mode: Convert one interface to another interface.

2. Decorator pattern: Do not change the interface, only add responsibilities.

The above is the detailed content of Detailed explanation of graphic code about adapter pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn