Setup mode

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();
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2842 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.