Java design mode-Adapter mode

Source: Internet
Author: User

Preface

Adapter mode is the adaptation between the required functionality and the functionality to be provided, so that the two can be seamlessly interfaced. For example, real-life computer charging situation, if a friend abroad to travel, bought a computer abroad, back to the domestic suddenly want to charge the computer, found that the foreign voltage is not 220V, but 110V (here is hypothetical), how to do? Fortunately, foreign-bought computer equipped with a charge converter, you can convert the 220V voltage to 110V voltage, OK, the computer charging problem can be solved. The charge converter is the adapter that seamlessly connects the 220V voltage and converts it to a voltage of 110V. This is the adapter mode.

Examplea computer that needs to be recharged
<span style= "FONT-SIZE:18PX;" >public class Computer {/** * Charging function, requires 100V of voltage *  * @param power100vinterface */public void Chargecomputer (Power110vi Nterface power110vinterface) {System.out.println ("The computer is charging, voltage is:" + power110vinterface.prove110power ());}} </span>
which means that the computer needs an interface that can provide a 110V voltage.
<span style= "FONT-SIZE:18PX;" >public interface Power110vinterface {public int prove110power ();} </span>

But the only class that can provide only 220V of voltage
public class power220v {/** * provides 220V voltage * @return */public int prove220power () {return 220;}}

It's okay to think we have a charge converter (adapter)
<span style= "FONT-SIZE:18PX;" >public class Power110vadapter implements Power110vinterface {private power220v power220v;public power110vadapter ( power220v power220v) {this.power220v = power220v;} @Overridepublic int Prove110power () {return this.prove110vfrom220v ();} /** * can convert 220V voltage to 110V voltage *  * @return */private int prove110vfrom220v () {return this.power220v.prove220Power ()/2;}} </span>
 Okay, all right, the computer is finally up to the point.
<span style= "FONT-SIZE:18PX;" >public class Maintest {public static void main (string[] args) {//a computer computer computer=new computer ();// can provide 220V voltage power220v power220v=new power220v ();//adapter generates an interface that provides 110V voltage Power110vinterface power110vinterface=new Power110vadapter (power220v);//Computer charged Computer.chargecomputer (power110vinterface);}} </span>

results:
the computer is charging, the voltage is:

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