Java Adapter mode (adapter mode)

Source: Internet
Author: User

Adapter Mode definition: Jiuge two incompatible classes together, in a structured pattern, with two identities (adaptee) and adaptor (adapters). Why use adapter mode we often encounter the need to combine two classes that are not related to each other, the first solution is to modify the interfaces of the respective classes, but if we do not have the source code, or we do not want to modify the respective interfaces for an application. What to do?

Using adapter, create a hybrid interface (half-breed) between the two interfaces. How to use the adapter mode to implement the adapter method, in fact, "think in Java," The "Class regeneration" section has been mentioned, there are two ways: combination (composition) and Inheritance (inheritance),

Suppose we want to pile, there are two kinds: square pile round pile. public class squarepeg{
public void Insert (String str) {
System.out.println ("Squarepeg insert ():" +str);
}
}

public class roundpeg{
public void Insertintohole (String msg) {
System.out.println ("Roundpeg insertintohole ():" +msg);
}
There is now an application that requires both a square pile and a round pile to be played. Then we need to combine these two non-relational classes, assuming roundpeg we do not have source code, or source code we do not want to modify, then we use adapter to implement this application: public class Pegadapter extends squarepeg{
Private Roundpeg roundpeg;
Public Pegadapter (Roundpeg peg) (this.roundpeg=peg;)
public void Insert (String str) {roundpeg.insertintohole (str);}
In the above code, Roundpeg belongs to Adaptee, which is the appropriate ligand. The Pegadapter is adapter, adapting Adaptee (the adapter roundpeg) and Target (Squarepeg). In fact, this is the combination method (composition) and the Inheritance (inheritance) method is used synthetically.

Pegadapter first Inherits Squarepeg, then uses the combination of new to generate the object, generates Roundpeg object Roundpeg, and then overloads the parent class insert () method. From here, you also understand that using new to build objects differs from building objects using extends inheritance, which does not need to be modified for the original class, or even need to know its internal structure and source code.

If you have some experience with Java use, it has been found that this pattern is often used. Further use of the above pegadapter is inherited squarepeg, if we need to inherit from both sides, that is, inherit Squarepeg and inherit Roundpeg, because Java does not allow multiple inheritance, but we can implement (implements) two interfaces ( Interface):
    • public interface iroundpeg{
      public void Insertintohole (String msg);
      }
    • public interface isquarepeg{
      public void Insert (String str);
      }

The following is the new Roundpeg and Squarepeg, in addition to implementing the interface of the difference, and the above is no different. public class Squarepeg implements isquarepeg{
public void Insert (String str) {
System.out.println ("Squarepeg insert ():" +str);
}
}

public class Roundpeg implements iroundpeg{
public void Insertintohole (String msg) {
System.out.println ("Roundpeg insertintohole ():" +msg);
}
The following is the new pegadapter, called Two-way Adapter:public class Pegadapter implements iroundpeg,isquarepeg{
Private Roundpeg roundpeg;
Private Squarepeg squarepeg;

Construction method
Public Pegadapter (Roundpeg peg) {this.roundpeg=peg;}
Construction method
Public Pegadapter (Squarepeg peg) (this.squarepeg=peg;)

public void Insert (String str) {roundpeg.insertintohole (str);}
} There is also a kind of called pluggable adapters, can dynamically get a few adapters in one. Using reflection technology, you can dynamically discover public methods in a class.



Adapter mode Overview
    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.
Applicability
    1. You want to use a class that already exists, and its interface doesn't fit your needs.    2. You want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those that might not      necessarily be compatible with the interface).    3. (for object adapter only) you want to use some subclasses that already exist, but it is not possible to subclass each to      match their interfaces. The object adapter can be adapted to its parent class interface.
participants
    1.Target      defines a domain-specific interface used by the client.    2.Client      is in collaboration with objects that match the target interface.    3.Adaptee      defines an already existing interface that needs to be adapted.    4.Adapter      Adapter for Adaptee interface to target interface
class Diagram Example Target
Public interface Target {    void Adapteemethod ();        void Adaptermethod ();}
Adaptee
public class Adaptee {public    void Adapteemethod () {        System.out.println ("Adaptee method!");}    }
Adapter
public class Adapter implements Target {    private adaptee adaptee;        Public Adapter (Adaptee adaptee) {        this.adaptee = adaptee;    } public void Adapteemethod () {Adaptee.adapteemethod ();} public void Adaptermethod () {System.out.println ("Adapter method!");}    }
Client
public class Test {public    static void Main (string[] args) {        target target = new Adapter (new Adaptee ());        Target.adapteemethod ();                Target.adaptermethod ();    }}
result
Adaptee method! Adapter method!

Java Adapter mode (adapter mode)

Related Article

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.