标签:适合 阶段 pre log -- stat spec bsp apt
public interface PinPlug{ void charge(); }
2.1类适配器
public interface PinPlug{ void charge(); }
1.描述:与台灯配套的插座接口
public class ThreePinPlug{ public void specialcharge(){ System.out.println("三孔插座充电"); } }
public class PinPluginAdapter extends ThreePinPlug implements PinPlug{ public void charge(){ System.out.println("两孔桥接"); super.specialcharge(); } }
public class Lamp { private PinPlug pinPlug; public Lamp(){ this.pinPlug=new PinPluginAdapter(); } public void Lignting() { if (pinPlug!=null) { pinPlug.charge(); System.out.println("照明"); }else{ System.out.println("没电"); } } public static void main(String[] args){ Lamp lamp=new Lamp(); lamp.Lignting(); } }
2.2对象适配器---组合的方式
public class PinPluginAdapter implements PinPlug{ ThreePinPlug threePinPlug=new ThreePinPlug(); public void charge(){ System.out.println("两孔桥接"); threePinPlug.specialcharge(); } }
配器模式不适合在详细设计阶段使用它,它是一种补偿模式,专用来在系统后期扩展、修改时所用。
标签:适合 阶段 pre log -- stat spec bsp apt
原文地址:http://www.cnblogs.com/yangyunnb/p/6478309.html