Design Mode: adapter Mode

Source: Internet
Author: User
Adapter mode what is adapter Mode
  • The adapter mode is a structural mode that allows two unmatched interfaces to work together.
  • The adapter mode allows two unmatched classes to work collaboratively by converting one interface type to another interface type that the client expects.
  • The adapter mode is also called the package.
    The original definition of the adapter mode in the Gang of Four book is as follows:

    Converts the interface type of a class to the interface type expected by another client.
    Adapters allow multiple classes to work together even if they are originally mismatched interface types.

Application scenarios of the adapter Mode
  • In this scenario, you purchased a portable notebook in India and recently moved to the UK. However, the British electronic outlets are different from those in India. Therefore, your notebook cannot work directly.
    You must purchase an adapter to charge your Indian notebook on a British outlet.

  • When you have a legacy interface that needs to be integrated with the new system, the new system cannot directly accept the work method of the legacy database. Since the legacy library is no longer developed, we need to use the adapter to push two different types of work.

  • When you use a Mac, you often need a adapter to connect to the projector in the conference room. This adapter is used to make the two items that are incompatible with Mac and projector work together.

Features of the adapter Mode
  • The client initiates a request to the adapter by calling the adapter method using the target interface.
  • The adapter converts requests to one or more calls of the adapter through the adapter interface.
  • The client receives the call result and does not perceive that an adapter is performing this conversion.
When to use the adapter Mode
  • If you want to match existing classes with their interface types, you can use the adapter mode.
  • If you want to create reusable classes to facilitate interface interaction between two unmatched classes.
Adapter mode example

Lloyd's bank is an international bank that provides global services. The overseas account holder's tax rate is 0.03%.
In India, it provides two types of accounts, normal and platinum. The tax law does not apply to Indian accounts.
Now, the offshore account cannot match the Indian account.
Therefore, you need to design an account adapter accountadapter to enable two different account types to continue working.

The interaction diagram of this example is as follows.
Here, the client only needs to callgetBalance()Method.
Adapter caller'sgetOffshoreBalance()Method and return the expected result from the client.
The getbalance () method in the adapter calculates the account balance by deducting the tax.

It also holds an instance of the adapter.
This completely decouples the client and the adaptation. Only the adapters know both of them (client and adapter ).

Offshoreaccount. Java
Package Org. byron4j. cookbook. designpattern. adapter;/*** offshore account */public class offshoreaccount {private double balance;/** tax rate */Private Static final double tax_rate = 0.04; Public offshoreaccount (final double balance) {This. balance = balance;} public double gettaxrate () {return tax_rate;} public double getoffshorebalance () {return balance;} public void debit (final double debit) {If (balance >=debit) {balance-= debit ;}} public void credit (final double credit) {balance ++ = balance ;}}
Account. Java
Package Org. byron4j. cookbook. designpattern. adapter;/*** account interface type */public interface account {/*** get account balance * @ return */Public double getbalance (); /*** overdraft allowed ** @ return */Public Boolean isoverdraftavailable ();/*** loan; increase in account balance after loan * @ Param credit */Public void credit (final double credit );}
Abstractaccount. Java
Package Org. byron4j. cookbook. designpattern. adapter; public class balance actaccount implements account {/*** account balance */private double balance;/*** overdraft allowed */private Boolean isoverdraftavailable; public balance actaccount (final double size) {This. balance = size ;}@ override public double getbalance () {return balance ;}@ override public Boolean balance () {return isoverdraftavailable;} public void setoverdraftavailable (Boolean isoverdraftavailable) {This. isoverdraftavailable = isoverdraftavailable;} @ override Public String tostring () {return getclass (). getsimplename () + "balance =" + getbalance () + "overdraft:" + isoverdraftavailable () ;}@ override public void credit (final double credit) {balance + = credit ;}}
Platinumaccount. Java
Package Org. byron4j. cookbook. designpattern. adapter;/*** platinum account: overdraft */public class platinumaccount extends actaccount {public platinumaccount (final double balance) {super (balance ); // setoverdraftavailable (true );}}
Standardaccount. Java
Package Org. byron4j. cookbook. designpattern. adapter;/*** normal account: cannot overdraw */public class standardaccount extends actaccount {public standardaccount (final double balance) {super (balance ); // cannot overdraw setoverdraftavailable (false );}}
Accountadapter. Java
Package org. byron4j. Cookbook. designpattern. Adapter;/*** account adapter; the adapter inherits from the target account. * The adapter aims to convert an offshore account (a Mac computer in the conference room) into an actaccount (Projector can be connected) */public class accountadapter extends actaccount {/** account to be adapted-adapter */private offshoreaccount; /***** @ Param offshoreaccount adapter-Mac computer in the conference room */Public accountadapter (final offshoreaccount) {super (offshoreaccount. getoffshorebalance (); // The adapter holds the reference of the adapter this. offshoreaccount = offshoreaccount;}/*** calculate the offshore account balance after tax deduction * @ return */@ override public double getbalance () {// offshore tax rate final double taxrate = offshoreaccount. gettaxrate (); // offshore account balance final double grossbalance = offshoreaccount. balance (); // The tax to be deducted final double taxablebalance = grossbalance * taxrate; // the account balance final double balanceaftertax = grossbalance-taxablebalance; return balanceaftertax ;}}
Adaptertest. Java
package org.byron4j.cookbook.designpattern;import org.byron4j.cookbook.designpattern.adapter.AccountAdapter;import org.byron4j.cookbook.designpattern.adapter.OffshoreAccount;import org.byron4j.cookbook.designpattern.adapter.StandardAccount;import org.junit.Test;public class AdapterTest {    @Test    public void test(){        StandardAccount sa = new StandardAccount(2000);        System.out.println("Account Balance= " + sa.getBalance());        //Calling getBalance() on Adapter        AccountAdapter adapter = new AccountAdapter(new OffshoreAccount(2000));        System.out.println("Account Balance= " + adapter.getBalance());    }}

Design Mode: 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.