适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适配器模式Demo:
/** * 2018年3月31日下午10:29:27 */ package com.Designpattern; /** * @author xinwenfeng * */ public class TestAdapter { /** * @param args */ public static void main(String[] args) { Target t = new Adapter_110vTo220v(); t.voltageOf110(); } } interface Target{ public void voltageOf110();//客户只提供110v输出 } //需要适配的类 class Adaptee_220V{ public void voltageOf220() { System.out.println("输出220v电压");//只能使用220v输出 } } //适配器 class Adapter_110vTo220v implements Target{ private Adaptee_220V v220 = new Adaptee_220V(); @Override public void voltageOf110() { System.out.println("转换:110v-->>220v"); v220.voltageOf220(); } }
结果: