Design Mode-10 adapter Mode

Source: Internet
Author: User

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 ..



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.