/*
*
* 适配器模式(Adapter)
*
*
* 用一个适配器类把一个接口转成另一个接口。
*/
package model;
public class TestAdapter {
public static void main(String[] args) {
USB mouse = new Mouse();
PC pc = new PC();
// pc.useMouse(mouse);
PS2 adapter = new USB2PS2Adapter(mouse);
pc.useMouse(adapter);
}
}
interface PS2 {
void usePs2();
}
interface USB {
void useUsb();
}
class Mouse implements USB {
public void useUsb() {
System.out.println("通锟斤拷USB锟接口癸拷锟斤拷");
}
}
class PC {
public void useMouse(PS2 ps2Mouse) {
ps2Mouse.usePs2();
}
}
class USB2PS2Adapter implements PS2 {
private USB usb;
public USB2PS2Adapter(USB usb) {
this.usb = usb;
}
public void usePs2() {
System.out
.println("锟窖讹拷usePS2锟侥凤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟缴讹拷useUSB锟侥凤拷锟斤拷锟斤拷锟斤拷");
usb.useUsb();
}
}
设计模式==适配器模式(Adapter)
最新推荐文章于 2024-09-07 23:46:48 发布