适配器模式
以三孔插座和两孔插座为例
新建一个三孔插座的接口
public interface ThreeElectricOutlet{
public abstract void connectElectricCurrent();
}
两孔插座
public interface TwoElectricOutlet {
public abstract void connectElectricCurrent();
}
创建一个适配器类TreeElectricAdapter.java
理解为两孔转三孔的转换器
public class TreeElectricAdapter implements TwoElectricOutlet{
ThreeElectricOutlet outlet;
TreeElectricAdapter(ThreeElectricOutlet outlet){
this.outlet=outlet;
}
public void connectElectricCurrent(){
outlet.connectElectricCurrent();
}
}
编写测试类
public class Application{
public static void main(String args[]){
TwoElectricOutlet outlet;
//洗衣机是三孔
Wash wash=new Wash();
TreeElectricAdapter adapter=new TreeElectricAdapter(wash);
outlet=adapter;
System.out.println("使用三相插座接通电流:");
outlet.connectElectricCurrent();
//电视 2
TV tv=new TV();
outlet=tv;
System.out.println("使用2相插座接通电流:");
outlet.connectElectricCurrent();
//电风扇 2
Fan fan = new Fan();
outlet=fan;
System.out.println("使用2相插座接通电流:");
outlet.connectElectricCurrent();
//吹风机 2
HairDryer hairDryer = new HairDryer();
outlet=hairDryer;
System.out.println("使用2相插座接通电流:");
outlet.connectElectricCurrent();
//空调 3
Air air=new Air();
TreeElectricAdapter adapter1=new TreeElectricAdapter(air);
outlet=adapter1;
System.out.println("使用三相插座接通电流:");
outlet.connectElectricCurrent();
/* Fan fan = new Fan();
TreeElectricAdapter adapter1=new TreeElectricAdapter(fan);
outlet=adapter1;
System.out.println("使用三相插座接通电流:");
outlet.connectElectricCurrent();*/
}
}
//创建几种电器的类
class Wash implements ThreeElectricOutlet{
String name;
Wash(){
name="黄河洗衣机";
}
Wash(String s){
name=s;
}
public void connectElectricCurrent(){
turnOn();
}
public void turnOn(){
System.out.println(name+"开始洗衣物。");
}
}
class TV implements TwoElectricOutlet{
String name;
TV(){
name="长江电视机";
}
TV(String s){
name=s;
}
public void connectElectricCurrent(){
turnOn();
}
public void turnOn(){
System.out.println(name+"开始播放节目。");
}
}
class Fan implements TwoElectricOutlet{
String name;
Fan(){name="美的风扇";}
Fan(String s){name=s;}
public void connectElectricCurrent(){turnOn();}
public void turnOn(){
System.out.println(name+"开始吹风");
}
}
class HairDryer implements TwoElectricOutlet{
String name;
HairDryer(){name="戴森吹风机";}
HairDryer(String s){name=s;}
public void connectElectricCurrent(){turnOn();}
public void turnOn(){
System.out.println(name+"开始吹头发");
}
}
class Air implements TwoElectricOutlet{
String name;
Air(){name="格力空调";}
Air(String s){name=s;}
public void connectElectricCurrent(){turnOn();}
public void turnOn(){
System.out.println(name+"开始吹空调");
}
}
运行结果
学不明白怎么办
/***
* .,:,,, .::,,,::.
* .::::,,;;, .,;;:,,....:i:
* :i,.::::,;i:. ....,,:::::::::,.... .;i:,. ......;i.
* :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.
* ;;..,::::;;;;ri,,,. ..,,:;s1s1ssrr;,.;r,
* :;. ,::;ii;:, . ................... .;iirri;;;,,;i,
* ,i. .;ri:. ... ............................ .,,:;:,,,;i:
* :s,.;r:... ....................................... .::;::s;
* ,1r::. .............,,,.,,:,,........................,;iir;
* ,s;........... ..::.,;:,,. ...............,;1s
* :i,..,. .,:,,::,. .......... .......;1,
* ir,....:rrssr;:, ,,.,::. .r5S9989398G95hr;. ....,.:s,
* ;r,..,s9855513XHAG3i .,,,,,,,. ,S931,.,,.;s;s&BHHA8s.,..,..:r:
* :r;..rGGh, :SAG;;G@BS:.,,,,,,,,,.r83: hHH1sXMBHHHM3..,,,,.ir.
* ,si,.1GS, sBMAAX&MBMB5,,,,,,:,,.:&8 3@HXHBMBHBBH#X,.,,,,,,rr
* ;1:,,SH: .A@&&B#&8H#BS,,,,,,,,,.,5XS, 3@MHABM&59M#As..,,,,:,is,
* .rr,,,;9&1 hBHHBB&8AMGr,,,,,,,,,,,:h&&9s; r9&BMHBHMB9: . .,,,,;ri.
* :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr. ...,:rs.
* ;s. .:sS8G8GG889hi. ....,,:;:,.:irssrriii:,. ...,,i1,
* ;1, ..,....,,isssi;, .,,. ....,.i1,
* ;h: i9HHBMBBHAX9: . ...,,,rs,
* ,1i.. :A#MBBBBMHB##s ....,,,;si.
* .r1,.. ,..;3BMBBBHBB#Bh. .. ....,,,,,i1;
* :h;.. .,..;,1XBMMMMBXs,.,, .. :: ,. ....,,,,,,ss.
* ih: .. .;;;, ;;:s58A3i,.. ,. ,.:,,. ...,,,,,:,s1,
* .s1,.... .,;sh, ,iSAXs;. ,. ,,.i85 ...,,,,,,:i1;
* .rh: ... rXG9XBBM#M#MHAX3hss13&&HHXr .....,,,,,,,ih;
* .s5: ..... i598X&&A&AAAAAA&XG851r: ........,,,,:,,sh;
* . ihr, ... . .. ........,,,,,;11:.
* ,s1i. ... ..,,,..,,,.,,.,,.,.. ........,,.,,.;s5i.
* .:s1r,...................... ..............;shs,
* . .:shr:. .... ..............,ishs.
* .,issr;,... ...........................,is1s;.
* .,is1si;:,....................,:;ir1sr;,
* ..:isssssrrii;::::::;;iirsssssr;:..
* .,::iiirsssssssssrri;;:.
*/