Adapter (Adapter mode)

Source: Internet
Author: User

Adapter mode:

Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.

There are two types of adapter modes:

1) class adapter (by inheritance)

2) object Adapter (Takes object combination mode)

--------------------------Class Adapter-----------------------------

Target.java

[Java]View plain copy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Package com.adapter;
    2. Public Interface Target
    3. {
    4. public void Method ();
    5. }

by adapter class

Adaptee.java

[Java]View plain copy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Package com.adapter;
    2. Public class Adaptee
    3. {
    4. public void Method2 ()
    5. {
    6. System.out.println ("adapter-->method2 ()");
    7. }
    8. }

Adapter Class Adapter.java

[Java]View plain copy
    1. Package com.adapter;
    2. Public class Adapter extends Adaptee implements Target
    3. {
    4. public Void Method ()
    5. {
    6. super.method2 ();  //or THIS.METHOD2 ();
    7. }
    8. }

Client.java

[Java]View plain copy
    1. Package com.adapter;
    2. Public class Client
    3. {
    4. public static void Main (string[] args)
    5. {
    6. Target t = new Adapter ();
    7. T.method ();
    8. }
    9. }

--------------------------Object Adapter-----------------------------

Target.java

[Java]View plain copy
    1. Package com.adapter;
    2. Public Interface Target
    3. {
    4. public void Method ();
    5. }

by adapter class

Adaptee.java

[Java]View plain copy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Package com.adapter;
    2. Public class Adaptee
    3. {
    4. public void Method2 ()
    5. {
    6. System.out.println ("adapter-->method2 ()");
    7. }
    8. }

Adapter Class Adapter.java

[Java]View plain copy
  1. Package com.adapter;
  2. Public class Adapter implements Target
  3. {
  4. private Adaptee adaptee;
  5. Public Adapter (adaptee adaptee)
  6. {
  7. this.adaptee = adaptee;
  8. }
  9. public Void Method ()
  10. {
  11. this.adaptee.method2 ();
  12. }
  13. }

Client.java

[Java]View plain copy
  1. Package com.adapter;
  2. Public class Client
  3. {
  4. public static void Main (string[] args)
  5. {
  6. Adaptee adaptee = new Adaptee ();
  7. Target t = new Adapter (adaptee);
  8. T.method ();
  9. }
  10. }

Adapter (Adapter mode)

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.