Adapter mode:
1. Class adapter Mode
Source role (Adaptee ):
Package com. classadpter;
Public class Adaptee {
Public void code (){
System. out. println ("code writing ");
}
}
Target abstract role (Target ):
Package com. classadpter;
Public interface Target {
Public void code ();
Public void pm ();
}
Adapter role)
Package com. classadpter;
// Adpter: inherits the Target and Adaptee
Public class Adapter extends Adaptee implements Target {
// Here this IT migrant workers can write code and want to make products. Many coder users like this.
@ Override
Public void pm (){
// TODO Auto-generated method stub
System. out. println ("still available products ");
}
Public void work (){
This. code ();
This. pm ();
}
Public static void main (String [] args ){
Adapter adapter = new Adapter ();
Adapter. work ();
}
}
Test result:
Write code
You can also make products
2. Object Adapter Mode
Source role (Adaptee ):
Package com. objectadapter;
Public class Adaptee {
Public void code (){
System. out. println ("code writing ");
}
}
Target abstract role (Target ):
Package com. objectadapter;
Public interface Target {
Public void code ();
Public void pm ();
}
Adapter role)
Package com. objectadapter;
// Adpter: inherits the Target and Adaptee
Public class Adapter implements Target {
Public Adaptee adaptee;
// This IT migrant workers can write code and make products, so many coder users like this.
Public Adapter (Adaptee adaptee ){
This. adaptee = adaptee;
}
Public void code (){
// TODO Auto-generated method stub
This. adaptee. code ();
}
Public void pm (){
System. out. println ("I can still make products ");
}
Public void work (){
This. code ();
This. pm ();
}
Public static void main (String [] args ){
Adaptee adaptee = new Adaptee ();
Adapter adapter = new Adapter (adaptee );
Adapter. work ();
}
}