洪洞网站建设,养老保险网站,网站建站制作价格,苏州 网站制作公司说明#xff1a;本文介绍设计模式中结构型设计模式中的#xff0c;适配器模式#xff1b;
插头转换器
适配器模式属于结构型设计模式#xff0c;设计思想体现在结构上的。以插头转换器为例#xff0c;当你需要给手机充电#xff0c;但是眼前只有一个三孔插座#xff0…说明本文介绍设计模式中结构型设计模式中的适配器模式
插头转换器
适配器模式属于结构型设计模式设计思想体现在结构上的。以插头转换器为例当你需要给手机充电但是眼前只有一个三孔插座二脚充电器无法使用三孔插座。
这时如果有一个插头转换器可以将二孔插头转为三脚插头就可以解决眼前问题。这个转换器扮演的就是适配器的角色。如下
TwoPin二脚插座接口只提供二脚插头充电
/*** 二脚插座*/
public interface TwoPin {/*** 二脚充电*/public void charge(int l, int r);
}ThreePin三脚插座接口只提供三脚插头充电
/*** 三脚插座*/
public interface ThreePin {/*** 三脚充电*/public void charge(int l, int r, int g);
}Phone手机充电器属于二脚插头
/*** 手机*/
public class Phone implements TwoPin{Overridepublic void charge(int l, int r) {System.out.println(手机充电中... l r);}
}客户端类演示手机充电过程二孔插头无法插入三孔插座类型错误 为了解决上面问题我们可以创建一个适配器类用来适配手机类使用三孔插座如下
/*** 适配器*/
public class Adapter implements ThreePin{/*** 适配器持有手机对象*/private Phone phone;/*** 适配手机使用三脚插座* param l* param r* param g*/Overridepublic void charge(int l, int r, int g) {phone.charge(l, r);}
}Client客户端演示手机使用三脚插座
/*** 客户端*/
public class Client {public static void main(String[] args) {// 借助适配器手机使用三脚插座充电Phone phone new Phone();new Adapter(phone).charge(1, 1, 0);}
}借助适配器手机就可以使用三脚插座了 专属适配器
当我们想让该适配器专属于手机而不给其他类使用时我们可以通过继承的方式来实现。
如下
Phone手机类给手机一个name属性表示手机的名称
/*** 手机*/
public class Phone implements TwoPin{/*** 手机名称*/protected String name;public Phone(String name) {this.name name;}Overridepublic void charge(int l, int r) {System.out.println(name 手机充电中... l r);}
}PhoneAdapter手机专属适配器继承于手机实现三脚插座接口
/*** 手机专属适配器*/
public class PhoneAdapter extends Phone implements ThreePin{/*** 适配器持有手机对象*/private Phone phone;public PhoneAdapter(Phone phone) {super(phone.name);this.phone phone;}Overridepublic void charge(int l, int r, int g) {System.out.println(name 在使用专属的适配器给手机充电...);super.charge(l, r);}
Client客户端演示小米手机使用手机专属的适配器用三孔插座充电
/*** 客户端*/
public class Client {public static void main(String[] args) {// 小米手机使用手机专属的适配器用三孔插座充电Phone phone new Phone(小米手机);new PhoneAdapter(phone).charge(1, 1, 0);}
}双向适配器
双向适配器就是该适配器对二孔插头、三孔插头的充电器都可以适配如下 代码实现如下
MultipleAdapter多重适配器实现两个接口注入两个对象创建对应的两个构造方法
/*** 多重适配器*/
public class MultipleAdapter implements ThreePin, TwoPin{/*** 三孔插座*/private ThreePin threePin;/*** 二孔插座*/private TwoPin twoPin;public MultipleAdapter(ThreePin threePin) {this.threePin threePin;}public MultipleAdapter(TwoPin twoPin) {this.twoPin twoPin;}/*** 三孔插座充电* param l* param r* param g*/Overridepublic void charge(int l, int r, int g) {this.twoPin.charge(l, r);}/*** 二孔插座充电* param l* param r*/Overridepublic void charge(int l, int r) {this.threePin.charge(l, r, 0);}
}Computer创建一个电脑类使用三孔插座的
/*** 电脑*/
public class Computer implements ThreePin{/*** 电脑名称*/protected String name;public Computer(String name) {this.name name;}Overridepublic void charge(int l, int r, int g) {System.out.println(name 充电中... l r g);}
}Client客户端演示手机、电脑使用多重适配器给手机充电
/*** 客户端*/
public class Client {public static void main(String[] args) {// 电脑使用多重适配器用二孔插座充电Computer computer new Computer(联想电脑);new MultipleAdapter(computer).charge(1,1);// 手机使用多重适配器用三孔插座充电Phone phone new Phone(小米手机);new MultipleAdapter(phone).charge(1,1,0);}
}不论是二孔插头还是三孔插头都行 以上就是适配器模式的内容适配的思想还是非常常见的在Java中方法重载不正是适配器模式在方法上的一种体现吗而适配器模式是在类的层面上对多种类、对象之间的一种适配。
总结
本文参考《设计模式的艺术》、《秒懂设计模式》两书