Hello everyone, let me talk about the adapter mode today.
Let's first look at the concept of the adapter:
The adapter provided by Baidu encyclopedia is an interface converter. It can be an independent hardware interface device that allows hardware or electronic interfaces to be connected to other hardware or electronic interfaces, or information interfaces.
From this concept, we can see that the adapter is a conversion thing.
For example, China's voltage standard is 220 V. The power supply voltage of our computer is usually 19 V (my computer is 20 V ). obviously, the voltage of V is applied to the power supply of V. If it is not processed, our power supply is estimated to be decommissioned. So how to do it? The answer is an adapter. Our power adapter helped us to convert the power, so that the V power supply can be used for the 19V computer power supply.
Simply put, the adapter converts information
In the process of software development, we often encounter this situation.
We use interfaces to provide the services provided by the ingress class, but some existing classes can already complete this service, but its interfaces are not necessarily what we want. In this case, existing interfaces need to be converted to the desired interfaces of the customer class, which ensures reuse of existing classes. Without such conversion, the customer class cannot take advantage of the functions provided by the existing class, And the adapter mode can complete this conversion.
Okay. Let's take a look at the diagram of the two classic adapter modes.
First, class Adapter
Next, the Object Adapter
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + 0 s/Signature + signature + sLrsMk8L3A + signature/Signature/H67 + 0tPrC6zwvcD4KPHA + signature/Signature =" brush: java; "> package edu. fjnu. cs. hwb. adapter; public interface Operation {void cutImage ();}
package edu.fjnu.cs.hwb.adapter;public class ImageOperationAdaptee{public void shearImage(){System.out.println("ImageOperation's shearImage()");}}
package edu.fjnu.cs.hwb.adapter;public class ImageOperationAdapter extends ImageOperationAdaptee implements Operation{@Overridepublic void cutImage(){// TODO Auto-generated method stubshearImage();}}
package edu.fjnu.cs.hwb.adapter;public class Client{public static void main(String[] args){Operation operation = new ImageOperationAdapter();operation.cutImage();}}
Result:
I asked the teacher why the Object Adapter must use inheritance instead of implementation. the teacher told me: "This thing has a mathematical model. It is better to use inheritance after verification. I have read this paper before "..
The landlord is decisive and weak ..