适配器模式主要用于将一个类的接口转化成客户端希望的目标类格式,使得原本不兼容的类可以在一起工作,将目标类和适配者类解耦;同时也符合“开闭原则”,可以在不修改原代码的基础上增加新的适配器类;将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性,但是缺点在于更换适配器的实现过程比较复杂。
结构型
已存在的类,它的方法和需求不匹配时(方法结果相同或相似)
提高类的透明性和复用,现有的类复用但不需要改变
目标类和适配器类解耦,提高程序扩展性
符合开闭原则
适配器编写过程需要全面考虑,会增加系统的复杂性
增加系统阅读的阅读行
对象适配器
类适配器
适配器模式和外观模式都是对现有系统的封装,外观模式是定义新的接口,适配器模式是复用原有的接口,适配器模式是使已有的两个接口协同工作,而外观模式是提供一个更为方便的入口。
package com.maidou.learning.design.structure.adapter.classadapter;public class Adaptee {public void adapteeRequest() {System.out.println("被适配者");}
}
package com.maidou.learning.design.structure.adapter.classadapter;public interface Target {void request();
}
package com.maidou.learning.design.structure.adapter.classadapter;public class ConcreteTarget implements Target{@Overridepublic void request() {System.out.println("被适配者");}
}
package com.maidou.learning.design.structure.adapter.classadapter;public class Adapter extends Adaptee implements Target{@Overridepublic void request() {super.adapteeRequest();}
}
测试
package com.maidou.learning.design.structure.adapter.classadapter;public class MainTest {public static void main(String[] args) {Target target = new ConcreteTarget();target.request();Target target1 = new Adapter();target1.request();}
}
package com.maidou.learning.design.structure.adapt1er.objectadapter;public class Adapter implements Target{private Adaptee adaptee = new Adaptee();@Overridepublic void request() {adaptee.adapteeRequest();}
}
protected XmlAdapter() {}/*** Convert a value type to a bound type.** @param v* The value to be converted. Can be null.* @throws Exception* if there's an error during the conversion. The caller is responsible for* reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.*/public abstract BoundType unmarshal(ValueType v) throws Exception;/*** Convert a bound type to a value type.** @param v* The value to be convereted. Can be null.* @throws Exception* if there's an error during the conversion. The caller is responsible for* reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.*/public abstract ValueType marshal(BoundType v) throws Exception;
实行xml的序列化方法和反序列方法进行数据的传输
org.springframework.aop.framework.adapter
public interface AdvisorAdapter {boolean supportsAdvice(Advice advice);MethodInterceptor getInterceptor(Advisor advisor);
}
实现方法
class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {MethodBeforeAdviceAdapter() {}public boolean supportsAdvice(Advice advice) {return advice instanceof MethodBeforeAdvice;}public MethodInterceptor getInterceptor(Advisor advisor) {MethodBeforeAdvice advice = (MethodBeforeAdvice)advisor.getAdvice();return new MethodBeforeAdviceInterceptor(advice);}
}
org.springframework.orm.jpa
public interface JpaVendorAdapter {PersistenceProvider getPersistenceProvider();@Nullabledefault String getPersistenceProviderRootPackage() {return null;}default Map getJpaPropertyMap(PersistenceUnitInfo pui) {return this.getJpaPropertyMap();}default Map getJpaPropertyMap() {return Collections.emptyMap();}@Nullabledefault JpaDialect getJpaDialect() {return null;}default Class extends EntityManagerFactory> getEntityManagerFactoryInterface() {return EntityManagerFactory.class;}default Class extends EntityManager> getEntityManagerInterface() {return EntityManager.class;}default void postProcessEntityManagerFactory(EntityManagerFactory emf) {}default void postProcessEntityManager(EntityManager em) {}
}