Java design mode adapter mode (adapter mode) Introduction _java

Source: Internet
Author: User
Tags inheritance

Adapter Pattern Definition: Use two incompatible classes Jiuge, belong to the structure mode, need to have adaptee (be fit) and adaptor (adapter) two identity.

Why use Adapter mode

We often run into a combination of two classes that don't have a relationship, the first solution is to modify the interfaces of the respective classes, but if we don't have the source code, or we don't want to modify our interfaces for an application. What to do?

Using adapter, create a mixed interface (half-breed) between the two interfaces.

How to use adapter mode

The implementation of the adapter approach, in fact, in the "Class regeneration" section of "Think in Java", has been mentioned in two ways: combination (composition) and Inheritance (inheritance),

Suppose we are piling, there are two kinds: square pile round pile.

Copy Code code as follows:

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

Now there is an application that needs to play both the square pile and the round pile. So we need to combine these two not-related classes, assuming roundpeg we don't have source code, or we don't want to modify the source code, then we use adapter to implement this application:

Copy Code code as follows:

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 code above, Roundpeg belongs to the Adaptee and is the fittest. Pegadapter is a adapter that matches Adaptee (the roundpeg of the fittest) and target (destination squarepeg). In fact, this is a combination of combining methods (composition) and inheritance (inheritance) methods.

Pegadapter first inherits the Squarepeg and then uses the combination of new to generate objects, generate Roundpeg object Roundpeg, and then overload the parent class insert () method. From here, you also understand the difference between using new to generate objects and using extends to inherit the objects, without having to modify the original class, even without having 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

The above Pegadapter is inherited squarepeg, if we need to inherit both sides, that is, inherit Squarepeg and inherit Roundpeg, because more inheritance is not allowed in Java, but we can implement (implements) two interfaces (interface ):

Copy Code code as follows:

public interface iroundpeg{
public void Insertintohole (String msg);
}
public interface isquarepeg{
public void Insert (String str);
}

Here are the new roundpeg and Squarepeg, and the difference between implementing the interface is no different from the one above.

Copy Code code as follows:

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

Here is the new pegadapter, called Two-way adapter:

Copy Code code as follows:

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 called pluggable adapters, which can dynamically obtain one of several adapters. Using reflection technology, you can dynamically discover public methods in a class.

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.