當你想使用一個已經存在的類,而它的接口不符合你的需求,或者你想建立一個可重用的類(與不兼容接口無關的類),這時候能夠考慮使用適配器模式。同時它也是一種包裝模式,它與裝飾模式一樣具備包裝的功能java
class M4DataLine { public void connection() { System.out.println("使用小米4數據線鏈接..."); } }
interface Target { void connection(); } class M5DataLine implements Target { @Override public void connection() { System.out.println("使用小米5數據線鏈接..."); } }
class M5DataLineAdapter extends M4DataLine implements Target { @Override public void connection() { System.out.println("插入 type-c 轉接頭"); super.connection(); } }
public class AdapterMain { public static void main(String[] args) { Target target = new M5DataLine(); target.connection(); Target adapter = new M5DataLineAdapter(); adapter.connection(); } }
使用小米5數據線鏈接... 插入 type-c 轉接頭 使用小米4數據線鏈接...
class M5DataLineAdapter implements Target { private Target target; public M5DataLineAdapter(Target target) { this.target = target; } @Override public void connection() { System.out.println("插入 type-c 轉接頭"); target.connection(); } } public class AdapterMain { public static void main(String[] args) { // 使用特殊功能類,即適配類 Target adapter = new M5DataLineAdapter(new M5DataLine()); adapter.connection(); } }
使用適配器模式的類編程
java.util.Arrays#asList() java.io.InputStreamReader(InputStream) java.io.OutputStreamWriter(OutputStream)
如下是源碼示例:數組
public interface MethodBeforeAdvice extends BeforeAdvice { void before(Method method, Object[] args, Object target) throws Throwable; } public interface AdvisorAdapter { boolean supportsAdvice(Advice advice); MethodInterceptor getInterceptor(Advisor advisor); } class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable { @Override public boolean supportsAdvice(Advice advice) { return (advice instanceof MethodBeforeAdvice); } @Override public MethodInterceptor getInterceptor(Advisor advisor) { MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice(); return new MethodBeforeAdviceInterceptor(advice); } }
默認的適配器註冊表:ide
public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable { private final List<AdvisorAdapter> adapters = new ArrayList<AdvisorAdapter>(3); public DefaultAdvisorAdapterRegistry() { // 註冊適配器 registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); } @Override public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) { return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) { throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) { // So well-known it doesn't even need an adapter. return new DefaultPointcutAdvisor(advice); } for (AdvisorAdapter adapter : this.adapters) { // 檢查是否支持,這裏調用了適配器的方法 if (adapter.supportsAdvice(advice)) { return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); } @Override public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException { List<MethodInterceptor> interceptors = new ArrayList<MethodInterceptor>(3); Advice advice = advisor.getAdvice(); if (advice instanceof MethodInterceptor) { interceptors.add((MethodInterceptor) advice); } for (AdvisorAdapter adapter : this.adapters) { // 檢查是否支持,這裏調用了適配器的方法 if (adapter.supportsAdvice(advice)) { interceptors.add(adapter.getInterceptor(advisor)); } } if (interceptors.isEmpty()) { throw new UnknownAdviceTypeException(advisor.getAdvice()); } return interceptors.toArray(new MethodInterceptor[interceptors.size()]); } @Override public void registerAdvisorAdapter(AdvisorAdapter adapter) { this.adapters.add(adapter); } }