Adapter mode in Java Design Mode

Source: Internet
Author: User

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

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.