设计模式之适配器模式
适配器模式(Adapter):
定义:把两个原本不兼容的类,使它们能够一起工作.
jdk中的应用:java.io.InputStreamRead.
源码中:DBUnits里面query方法.里面有2,3,4...
生活中的例子:电压.民用的是220v,手机充电器是5v,那充电器就相当于一个适配器
代码体现:
package com.mode.adapter;//对象适配器形式/*public class Tap {public void exeWater(){USATap tap = new USATap();tap.exeWater(0,0);}}*///类适配器形式public class Tap extends USATap{public void exeWater() {exeWater(0,0);}}package com.mode.adapter;public class USATap {public void exeWater(int i,int j){System.out.println("USATap.exeWater()");}}package com.mode.adapter;public class ChinaPipe {public static void main(String[] args) {Tap tap = new Tap();tap.exeWater();}}