标签:bsp factor 抽象 ack int class 客户 ring apt
需要一个网线类
package factory.adapter; //要被适配的类: 网线 public class Adaptee { public void request(){ System.out.println("拨号连接上网"); } }
package factory.adapter; //1,继承 (类适配器) //2,组合 (对象适配器) //真正的适配器~,需要连接USB,连接网线~ public class Adapter extends Adaptee implements NetToUsb { @Override public void handleRequest() { super.request();//可以上网了 } }
package factory.adapter; //接口 转换器的抽象实现 public interface NetToUsb { //作用:处理请求, 网线=>usb public void handleRequest(); }
package factory.adapter; //客户端类: 想上网,插不上网线· public class Computer { //我们的电脑需要连接上我们的转接器才可以上网 public void net(NetToUsb adapter){ //上网的具体实现~, 找一个转接头 adapter.handleRequest(); } public static void main(String[] args) { //电脑,适配器,网线 Computer computer = new Computer(); Adaptee adaptee = new Adaptee();//网线 Adapter adapter = new Adapter();//转接器 computer.net(adapter); } }
package factory.adapter; //1,继承 (类适配器) //真正的适配器~,需要连接USB,连接网线~ public class Adapter2 implements NetToUsb { //2,组合 (对象适配器) private Adaptee adaptee; public Adapter2(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void handleRequest() { adaptee.request();//可以上网了 } }
修改computer类
package factory.adapter; //客户端类: 想上网,插不上网线· public class Computer { //我们的电脑需要连接上我们的转接器才可以上网 public void net(NetToUsb adapter){ //上网的具体实现~, 找一个转接头 adapter.handleRequest(); } public static void main(String[] args) { //电脑,适配器,网线 Computer computer = new Computer(); Adaptee adaptee = new Adaptee();//网线 // Adapter adapter = new Adapter();//转接器 Adapter2 adapter2 = new Adapter2(adaptee); computer.net(adapter2); } }
标签:bsp factor 抽象 ack int class 客户 ring apt
原文地址:https://www.cnblogs.com/yppaopao/p/13169763.html