1. Adapter mode:
The main function of the adapter mode is to convert an interface that cannot be used by a class into a usable interface;
< p> Similar to Apple’s cancellation of the 3.5 headphone jack and an adapter, this is the adapter;
Second, class adapter:
/ /Achieved by inheriting the adapted class and implementing standard interfaces;
/**< span style="color: #008000;">
* Target interface, or standard interface
**/
public interface Target {
void newConnector();
}
/**
* Existing classes that have special functions but do not meet our existing standard interfaces
**/
public class Adaptee {
public void oldConnector() {
System.out.println("3.5 headphone jack...");
}
}
/**
* The adapter class inherits the adapted class and implements the standard interface at the same time
**/
public class Adapter extends Adaptee implements Target {
@Override
public void newConnector() {
this.oldConnector();
System.out.println("Adapter, realize the adaptation of Lightning interface...");
}
}
public class Client {
public static void main(String[] args) {
Target adapter = new Adapter();
//Call special functions through the adapter
adapter.newConnector();
}
}
Two, through the construction method delegation to achieve:
/**
* Target interface, or standard interface
**/
public interface Target {
void newConnector();
}
/**
* Existing classes that have special functions but do not meet our existing standard interfaces
**/
public class Adaptee {
public void oldConnector() {
System.out.println("3.5 headphone jack...");
}
}
/**
* Adapter class
**/
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void newConnector() {
this.adaptee.oldConnector();
System.out.println("Adapter, realize the adaptation of Lightning interface...");
}
}
public class Client {
public static void main(String[] args) {
Target adapter = new Adapter(new Adaptee());
//Call special functions through the adapter
adapter.newConnector();
}
}
/**
* Target interface, or standard interface
**/
public interface Target {
void newConnector();
}
/**
* Existing classes that have special functions but do not meet our existing standard interfaces
**/
public class Adaptee {
public void oldConnector() {
System.out.println("3.5 headphone jack...");
}
}
/**
* The adapter class inherits the adapted class and implements the standard interface at the same time
**/
public class Adapter extends Adaptee implements Target {
@Override
public void newConnector() {
this.oldConnector();
System.out.println("Adapter, realize the adaptation of Lightning interface...");
}
}
public class Client {
public static void main(String[] args) {
Target adapter = new Adapter();
//Call special functions through the adapter
adapter.newConnector();
}
}
/**
* Target interface, or standard interface
**/
public interface Target {
void newConnector();
}
/**
* Existing classes that have special functions but do not meet our existing standard interfaces
**/
public class Adaptee {
public void oldConnector() {
System.out.println("3.5 headphone jack...");
}
}
/**
* Adapter class
**/
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void newConnector() {
this.adaptee.oldConnector();
System.out.println("Adapter, realize the adaptation of Lightning interface...");
}
}
public class Client {
public static void main(String[] args) {
Target adapter = new Adapter(new Adaptee());
//Call special functions through the adapter
adapter.newConnector();
}
}