Structural model
The central role: is to achieve loose coupling from the structure of the program, thus expanding the overall class structure, to solve bigger problems
Adapter pattern adapter
Examples of real life: converter
Adapter in two ways:
1, class adapter (inherited)
/ ** adapter required object
* @author sail entering code
*
* /
public class the Adaptee {
public void Request () {
System.out.println ( "CAN do something");
}
}
/ **
* adapter (adapter class)
* @author sail entering code
*
* /
public class the extends the Adaptee the implements the Target Adapter {
* adapter (adapter class)
* @author sail entering code
*
* /
public class the extends the Adaptee the implements the Target Adapter {
@Override
public void handleReq() {
super.request();
}
public void handleReq() {
super.request();
}
}
/ **
* Client
* @author sail entering code
*
* /
public class Client {
public void Test (the Target T) {
t.handleReq ();
}
public static void main (String [] args) {
Client new new = C Client ();
the Target Adapter new new = T ();
c.test (T);
}
}
* Client
* @author sail entering code
*
* /
public class Client {
public void Test (the Target T) {
t.handleReq ();
}
public static void main (String [] args) {
Client new new = C Client ();
the Target Adapter new new = T ();
c.test (T);
}
}
public interface Target {
public void handleReq();
public void handleReq();
}
2, Object Adapter (combined)
/ ** adapter required object
* @author sail entering code
*
* /
public class the Adaptee {
public void Request () {
System.out.println ( "CAN do something");
}
}
* @author sail entering code
*
* /
public class the Adaptee {
public void Request () {
System.out.println ( "CAN do something");
}
}
/ **
* Adapter (Object Adapter)
* @author sail entering code
*
* /
public class adapter2 the extends the Adaptee the implements the Target {
Private the Adaptee A;
@Override
public void handleReq () {
a.request ();
}
public adapter2 () {
}
public adapter2 (the Adaptee A) {
this.a = A;
}
}
* Adapter (Object Adapter)
* @author sail entering code
*
* /
public class adapter2 the extends the Adaptee the implements the Target {
Private the Adaptee A;
@Override
public void handleReq () {
a.request ();
}
public adapter2 () {
}
public adapter2 (the Adaptee A) {
this.a = A;
}
}
public class Client2 {
public void test(Target t) {
t.handleReq();
}
public static void main(String[] args) {
Client c=new Client();
Adaptee a=new Adaptee();
Target t=new Adapter2(a);
c.test(t);
}
}
public void test(Target t) {
t.handleReq();
}
public static void main(String[] args) {
Client c=new Client();
Adaptee a=new Adaptee();
Target t=new Adapter2(a);
c.test(t);
}
}
public interface Target {
public void handleReq();
public void handleReq();
}