Sometimes we need to convert some incompatible interfaces into a compatible interface, which requires an adapter mode. The simplest application scenario is that the phone charging plug is not compatible with the need to use an adapter to enable the phone to charge.
For example, mobile phones in China can be charged, in Germany because of the standard is not the same as the need to use an adapter to charge mobile phones, Germany is using a round port socket power supply, China is using three flat head socket power supply.
So, write two charging standard interfaces first. and write out its implementation class
Dbsocketinterface.java: German Standard interface
Package adapter;/** * German standard interface */public interface Dbsocketinterface {/** * This method is named: Power supply */void Powerwithtworound () with two round headers;}
Dbsocket.java: Implement charging function
Package adapter;/** * German socket */public class Dbsocket implements Dbsocketinterface {public void Powerwithtworound () {SYSTEM.O UT.PRINTLN ("power supply with two round head jacks");}}
Gbsocketinterface.java: GB Interface
Package adapter;/** * GB Interface */public interface Gbsocketinterface {/** * This method is named: Power supply */void Powerwiththreeflat () using three-item flat headers;}
Gbsocket GB Standard Charge
Package Adapter;public class Gbsocket implements gbsocketinterface{@Overridepublic void Powerwiththreeflat () { SYSTEM.OUT.PRINTLN ("power supply using three flat-head sockets");}}
Charge at a hotel in Germany, write a hotel class
The German standard charging standard for package Adapter;public class hotel {//Ryokan private Dbsocketinterface dbsocket;public Hotel () {};//constructor initializes public Hotel (Dbsocketinterface dbsocket) {this.dbsocket=dbsocket;} Method initializes public void Setsocket (Dbsocketinterface dbsocket) {this.dbsocket=dbsocket;}//hotel's charging socket public void charge () {DBSO Cket.powerwithtworound (); }}
Test if the hotel class is available, and then create a new test class Hoteltest
Package Adapter;public class Hoteltest {public static void main (string[] args) {dbsocketinterface dbsocket=new dbsocket () ; Hotel Hotel=new Hotel (dbsocket),//hotel Hotel=new Hotel ();//hotel.setsocket (Dbsocket); Hotel.charge ();}}
Test results:
Power supply with two round-head jacks
How to make GB standard charger can use the German standard socket? An adapter is required at this time. Create a new adapter class adapter, the adapter needs to implement the German standard interface
Package Adapter;public class Adapter implements Dbsocketinterface{private Gbsocketinterface gbsocket;//plug in GB charger public Adapter (Gbsocketinterface gbsocket) {this.gbsocket=gbsocket;} Charging with adapter @overridepublic void Powerwithtworound () {Gbsocket.powerwiththreeflat ();}}
Under test:
Package Adapter;public class Test {public static void main (string[] args) {//with GB charger Gbsocketinterface socket=new Gbsocket ();//go to Hotel Hotel=new Hotel ();//Get an adaptor and plug the GB charger into the adapter Adapter=new adapter (socket);//Insert Adapter Hotel.setsocket ( adapter);//Charge Hotel.charge ();}}
The result is:
Power supply using three-item flat head sockets
It can be recharged ...
Java design mode----Adapter Mode