package com.bjsxt.adapter; /** * 被适配的类 * (相当于例子中的,PS/2键盘) * @author Administrator * */ public class Adaptee { public void request(){ System.out.println("可以完成客户请求的需要的功能!"); } }
Adaptee
package com.bjsxt.adapter; /** * 客户端类 * (相当于例子中的笔记本,只有USB接口) * @author Administrator * */ public class Client { public void test1(Target t){ t.handleReq(); } public static void main(String[] args) { Client c = new Client(); Adaptee a = new Adaptee(); // Target t = new Adapter(); Target t = new Adapter2(a); c.test1(t); } }
Client
package com.bjsxt.adapter; public interface Target { void handleReq(); }
Target
package com.bjsxt.adapter; /** * 适配器 (类适配器方式) * (相当于usb和ps/2的转接器) * @author Administrator * */ public class Adapter extends Adaptee implements Target { @Override public void handleReq() { super.request(); } }
adapter 类适配器
package com.bjsxt.adapter; /** * 适配器 (对象适配器方式,使用了组合的方式跟被适配对象整合) * (相当于usb和ps/2的转接器) * @author Administrator * */ public class Adapter2 implements Target { private Adaptee adaptee; @Override public void handleReq() { adaptee.request(); } public Adapter2(Adaptee adaptee) { super(); this.adaptee = adaptee; } }
Adapter 对象适配器
时间: 2024-10-10 08:38:55