Adapter mode note-java Implementation (power Adapter example)

Source: Internet
Author: User

There are two adapter modes:

1. First

Interface called by the adapted Program

package AdapterDesign1;public interface Target {void get18V();void get33V();void get0V();}

Adapted object

package AdapterDesign1;public class Adaptee {protected int get220V(){return 220;}}

Adapter

Package AdapterDesign1; public class Adapter extends Adaptee implements Target {@ Overridepublic void get0V () {// you do not need to provide a home vertex that can be returned directly, that is, 0 system is returned. out. println ("no voltage") ;}@ Overridepublic void get18V () {System. out. println ("add" + this. get220V () + "adapted to 18 V");} @ Overridepublic void get33V () {System. out. println ("add" + this. get220V () + "adapted to 33 V ");}}

Test method:

Package AdapterDesign1; public class MyMain {/** Adapter Mode Simulation: power Adapter adaptee: Home point 220 V Target: Get safe voltage, 18 V Adapter: power Adapter */public static void main (String [] args) {Target t = new Adapter (); t. get18V (); t. get33V ();}}

2. Second

Interface called by the adapted Program

package AdapterDesign1;public interface Target {void get18V();void get33V();void get0V();}

Two adaptee objects to be adapted

Package AdapterDesign2; public class Adaptee1 {@ Overridepublic String toString () {return "" ;}protected int get220V () {return 220 ;}}

Package adapterdesign2; public class adaptee2 {@ overridepublic string tostring () {return "DC" ;}protected int get220v () {return 220 ;}}

Adapter

Package adapterdesign2; public class adapter implements target {adaptee1 adaptee1; adaptee2 adaptee2; Public adapter () {adaptee1 = new adaptee1 (); adaptee2 = new adaptee2 ();} @ overridepublic void get0v () {// you do not need to provide a home vertex for direct return, that is, return 0system. out. println ("no voltage") ;}@ overridepublic void get18v () {system. out. println ("use an adapter to adapt" + adaptee1 + adaptee1.get220v () + "to 18 V") ;}@ overridepublic void get33v () {system. out. println ("use an adapter to adapt" + adaptee2 + adaptee2.get220v () + "to 33 V ");}}

Test code:

Package adapterdesign2; public class mymain {/** adapter Mode Simulation: power adapter adaptee: Home point 220 V target: Get safe voltage, 18 V adapter: power adapter */public static void main (string [] ARGs) {target T = new adapter (); T. get18v (); T. get33v ();}}

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.