java23 design patterns (three) - adapter mode

First, the adapter mode

Reprinted: https://www.cnblogs.com/V1haoge/p/6479118.html

Adapter is a kind of middleware adaptation, which is present between the two do not match for both connections, the mismatch becomes match, which is usually understood that simple point adapter seen, the presence of the converter or the like .

Adapter in two modes: an adapter class, object adapter interface adapter

The former two in the realization of slightly different, as the role of the third interface adapter varied.

1, Class Adapter Mode:

Principle: The adapter functions to achieve through inheritance.

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 to p transit, this adapter interfaces a p to achieve our visit, so that we can continue to have access method (although it is not currently our food) current interface a, then B inherits class that implements the interface BB, so that we can adapters the method of accessing the interface B, P, and then we a method in an interface adapter P is a suitable method of direct reference to the BB, so that the completion of a simple adapter class.

See examples below: We adapter ps2 and usb example

ps2 Interface: Ps2

 public interface Ps2 {
     void isPs2();
 }

USB Interface: Usb

public interface Usb {
    void isUsb();
 }

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 Method: Clienter

public class Clienter {

    public static void main(String[] args) {
        Ps2 p = new Adapter();
        p.isPs2();
    }

}

show result:

USB port

Examples to explain:

  My hands have a ps2 plug devices, but only on the host usb plug socket, how to do it? Get hold of converter to convert ps2 plug USB plug to be ready for use.

  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

2, object adapter mode

Principle: is achieved through a combination of adapter functions.

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 to p transit, this adapter interfaces a p to achieve our visit, so that we can continue to have access method (although it is not currently our food) a current interface, and then define the private variables C (Object) (B interface adapter P, point variable name), and then define a constructor with parameters assigned to the object C, the object C and then call its methods using the B interface from the a interface in the method implementation.

See examples below: We still adapter ps2 and usb example

ps2 Interface: Ps2

public interface Ps2 {
     void isPs2();
 }

USB Interface: Usb

public interface Usb {
     void isUsb();
 }

USB interface category: Usber

public class Usber implements Usb {

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

}

Adapters: Adapter

public class Adapter implements Ps2 {
    
    private Usb usb;
    public Adapter(Usb usb){
        this.usb = usb;
    }
    @Override
    public void isPs2() {
        usb.isUsb();
    }

}

Test categories: Clienter

public class Clienter {

    public static void main(String[] args) {
        Ps2 p = new Adapter(new Usber());
        p.isPs2();
    }

}

show result:

USB port

Examples to explain:

  My hands have a ps2 plug devices, but only on the host usb plug socket, how to do it? Get hold of converter to convert ps2 plug USB plug to be ready for use.

  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

3, interface adapter mode

Principle: abstract class is achieved through the adaptation, which is adapted to adapt the other slightly above.

When there is such an interface, which defines the N many ways, and we now want to use a few of these methods, if we implement the interface directly, so we want to achieve all the way, even if we are only to the method does not require blanking (write only a pair of braces, not the specific methods to achieve) can also cause this type become bloated, call is not convenient, then we can use an abstract class as middleware, namely adapters, implement the interface with the abstract class, but in an abstract class all methods blanking, then we create inherit the abstract class, and a few ways you can rewrite that we need to use.

Destination Interface: A

public interface A {
    void a();
    void b();
    void c();
    void d();
    void e();
    void f();
}

Adapters: Adapter

public abstract class Adapter implements A {
    public void a(){}
    public void b(){}
    public void c(){}
    public void d(){}
    public void e(){}
    public void f(){}
}

Implementation class: Ashili

public  class Ashili the extends Adapter {
     public  void A () { 
        System.out.println ( "A realization method is called" ); 
    } 
    public  void d () { 
        System.out.println ( "d implemented method is called" ); 
    } 
}

Test categories: Clienter

public class Clienter {

    public static void main(String[] args) {
        A a = new Ashili();
        a.a();
        a.d();
    }

}

4, the adapter mode scenario

Class Adapter consistent with usage scene object adapter, the means to achieve only a slight difference between the two is mainly used in the following scenarios:

  (1) you want to use an existing class, but it does not comply with existing interface specifications, making it impossible to access directly, then indirectly create an adapter will be able to access methods in this class.

  (2) We have a class, would like to design a reusable class (may be multiple-access), we can create adapters to adapt this class to other classes do not provide a suitable interface.

  In fact, more than two scenes from two angles to describe a class of problem, and that's how you want to access is not suitable interface where a departure from the interface (to be accessed), a departure from the access (access initiative).

Interface Adapter usage scenarios:

  (1) want to use the interface or a certain method, but the interface has too many methods, must implement the interface when we want to use and implement all of these methods, you can use an abstract class to implement the interface, not on the method achieve (blank only), then we will extend this abstract class method to be implemented by way of rewriting want to use. This abstract class is the adapter.

Guess you like

Origin www.cnblogs.com/cq-yangzhou/p/10999570.html