Java Design Pattern learning 5 -- adapter pattern [original]

Source: Internet
Author: User
I wrote some experiences about the adapter mode yesterday. I simply deleted it because it was not clearly written.
Today, I stopped electricity for one day, and I couldn't write anything during the day ~~
Call in the evening and start writing.

I wrote the build mode last time. Now let's look at the adapter mode.
Let's talk about the transformer (adapter), which converts one voltage to another. Let's look at another idiom ". We often encounter the need to convert an interface of a class to another interface that the customer wants, that is, let a method in a class reflect the effect of a method in another class, or add the method of a class to another class. The first solution is to modify or add class interfaces, but if we do not have source code, or we do not want to modify interfaces for an application. What should I do?
The adapter is required at this time.
Let's take a look at the figure below:
  

Object Adapter Mode

Adapter mode of the class
  

In general, the customer accesses the target request () method and wants to obtain the specialrequest () method in adaptee.
The adapter inherits the target, so the customer can access the adapter. In the adapter, how can we get the specialrequest () method?
The adapter can obtain the () pecialrequest method in adaptee in two ways:
1. There is no specialrequest method in the adapter. It is only a transfer station, and it accesses adaptee to get specialrequest.
2. The specialrequest method exists in the adapter.
In method 1, it is the combination method, while method 2 is the inheritance method.
The first method is the Object Adapter, and the second method is the class adapter.
Let's talk about the Object Adapter and look at the following code:
Public class adapter extends target
{
Private adaptee;
Public adapter (adaptee)
{
Super ();
This. adaptee = adaptee;
}
Public void request ()
{
Adptee. specialrequest ();
}
}
The adapter inherits the target and reloads the target request method to obtain the specialrequest method.
In this case, the adapter is called an Object Adapter. In this case, isn't the adapter used to introduce the object?
An Object Adapter can adapt different adaptee types to the same target (the same adapter can adapt both the source class and its subclass to the Target Interface ).
The Object Adapter allows the target and adaptee to be completely independent. Only the adapter knows whether the target and adaptee exist.
The class adapter is described below.
The Object Adapter only inherits the target, but the adapter can also inherit the adaptee. Because Java does not allow many inheritance, interfaces are required.
Now, we turn adaptee into inheriting an interface, and the adapter also inherits the same interface, so that the specialrequest method exists in the adapter, and the customer can directly access Adapter. specialrequest,
Public interface iadaptee {
Public void specialrequest ();
......
}

Public class adaptee implements iadaptee {
Public void adaptee (){
//......
}
Public void specialrequest (){
//......
}
}

Public class adapter extends target implements iadaptee {

Private adaptee adptee;
Public adapter (adaptee adptee ){
This. adptee = adptee;
}
Public void specialrequest (){
Adptee. specialrequest ();
}
}

In this case, if target and adaptee have a method with the same name, the adapter cannot guarantee that the two methods have the same meaning or behavior. It does not know which method to use, this is very dangerous.

In my opinion, the function of the adapter only needs to be converted. I can see on the Internet what is the method of replacing the source class in the adapter, is it necessary to put it in the adapter? It's really a headache.

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.