Common design patterns-adapter patterns]

Source: Internet
Author: User

Frequently-used problems: for example, cell phones, MP3, and computer chargers, the conversion connectors are equivalent to one adapter. This data conversion is similar to the adapter mode.

 
Package myadapter;/*** @ description: adapter mode * @ author Potter * @ date 11:17:12 * @ version V1.0 */public class app {public static void main (string [] ARGs) {adapter = new adapter (); adapter. setpower (New Power (220); device phone = new phone (); phone. AC (adapter );}}

Power supply:

 
Package myadapter;/*** @ Description: Power Supply * @ author Potter * @ date 11:20:35 * @ version V1.0 */public class power {int V; // voltage public power (INT v) {This. V = V ;}}

Adapter class:

Package myadapter;/*** @ Description: * @ author Potter * @ date 2012-8-14 11:18:10 * @ version V1.0 */public class adapter {power; /** convert to an appropriate power supply to charge the device **/Public int convert (device) {If (device instanceof MP3) {power. V = 20;} else if (device instanceof phone) {power. V = 25;} return power. v;} public power getpower () {return power;} public void setpower (Power) {This. power = power ;}}

 

Device abstract class:

 
Package myadapter;/*** @ description: * @ author Potter * @ date 09:59:58 * @ version V1.0 */public abstract class device {/** charge * @ Param p voltage */public abstract void AC (adapter) ;}

MP3:

Package myadapter;/*** @ Description: * @ author Potter * @ date 11:18:39 * @ version V1.0 */public class MP3 extends device {@ overridepublic void AC (adapter) {system. out. println ("map3 charging, voltage:" + adapter. convert (this) + "v ");}}

Phone:

 
Package myadapter;/*** @ Description: * @ author Potter * @ date 11:18:29 * @ version V1.0 */public class phone extends device {@ overridepublic void AC (adapter) {system. out. println ("mobile phone charging, voltage is" + adapter. convert (this) + "v ");}}

Print result:

Mobile phone charging with a voltage of 25 V

 

This is my understanding of the adapter mode. If you think that your younger brother is not correct, please give pointers. Thank you.

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.