Design Mode 6 -- adapter Mode

Source: Internet
Author: User


1. Interpretation 1.1 Definition

Converts an interface of a class to another interface that the customer wants.


1.2 Analysis

The definition focuses on "Conversion". The following examples all reflect the principle of the adapter:

1. For overseas travel, the renminbi must be exchanged into foreign currency for normal circulation and use.

2. The voltage used by each electrical device is different. A transformer is needed to provide the corresponding voltage.

3. Because of the different languages in different countries, the translation profession has emerged to help you communicate smoothly.


2 code analysis 2.1 story about rice purchase

Credit Card swiping, when you are spending abroad, he will first deduct the corresponding foreign currency, and then convert the currency of the country to deduct. In this way, we do not need to exchange currency any more. It is very convenient.

For more information, see the example above. An American friend takes an American card and buys rice in China. However, in the system, only US dollars can be used to buy US rice. Then we can implement one, as shown in.

 

Target interface, the interface required by the client:

public interface ActionTarget {public void buyRiceInDollars(int pDollarValue);public void buyRiceInRMB(int pRMBValue);}
 

Classes to be adapted:

Public class rmbadapee {public void buyriceinrmb (INT prmbvalue) {int amount = prmbvalue/2; // The purchased action can be placed in the interface or parent class. System. Out. println ("in China, you can purchase" + prmbvalue + "RMB" + amount + "rice ");}}


Adapter, the core of this mode:

Public class dollaradapter implements actiontarget {private rmbadapee mrmbadapee; Public dollaradapter (rmbadapee prmbadapee) {mrmbadapee = prmbadapee;} public void merge (INT pvalue) {int amount = pvalue/1; // The purchased actions can be placed in the interface or parent class. System. out. println ("in the United States, you can purchase" + pvalue + "USD" + amount + "") ;}@ overridepublic void buyriceinrmb (INT pvalue) {int rmbvalue = pvalue * 6; system. out. println (pvalue + "USD convertible" + rmbvalue + "RMB"); mrmbadapee. buyriceinrmb (rmbvalue );}}

As you can see from the above buyriceinrmb function, we must pass in RMB to use this function. Therefore, we made a currency conversion in the adapter and then called this function to implement the function of buying Chinese rice in USD.

 

Test class:

public class AdapterTest {public static void main(String[] args) {int dollar = 10;DollarAdapter adapter = new DollarAdapter(new RMBAdapee());adapter.buyRiceInDollars(dollar);adapter.buyRiceInRMB(dollar);}}
 

Code: https://github.com/bird7310/DesignPatternExample.git

Package name: COM. ahacool. designpattern. Adapter


3. Conclusion

This article is very simple, because this mode is very common, as long as you understand the principle, other in-depth study can view other materials and books.

 

 

 

Design Mode 6 -- adapter Mode

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.