设计模式之适配器模式
文章目录
- 设计模式之适配器模式
- 1. 定义
- 2. 类型
- 3. 场景
- 4. 优点
- 5. 缺点
- 6. 适配器扩展
- 6. 相关适配器模式
- 7. coding
- 7.1 被 适配者方法
- 7.2 接口
- 7.3 接口的实现
- 7.4 适配者适配被适配者达到Target 目标
- 7.5 类适配器的实现方式,其他的都不变
- 8. 源码解析
- 8.1 XmlAdapter
- 8.1.1来源 javax.xml.bind.annotation.adapters
- 8.1.2coding
- 8.1.3 说明
- 8.2 AdvisorAdapter
- 8.2.1 来源
- 8.2.2 coding
- 8.3 JpaVendorAdapter
- 8.3.1 来源
- 8.3.2 coding
1. 定义
适配器模式主要用于将一个类的接口转化成客户端希望的目标类格式,使得原本不兼容的类可以在一起工作,将目标类和适配者类解耦;同时也符合“开闭原则”,可以在不修改原代码的基础上增加新的适配器类;将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性,但是缺点在于更换适配器的实现过程比较复杂。
2. 类型
结构型
3. 场景
已存在的类,它的方法和需求不匹配时(方法结果相同或相似)
4. 优点
提高类的透明性和复用,现有的类复用但不需要改变
目标类和适配器类解耦,提高程序扩展性
符合开闭原则
5. 缺点
适配器编写过程需要全面考虑,会增加系统的复杂性
增加系统阅读的阅读行
6. 适配器扩展
对象适配器
类适配器
6. 相关适配器模式
- 适配器模式和外观模式
适配器模式和外观模式都是对现有系统的封装,外观模式是定义新的接口,适配器模式是复用原有的接口,适配器模式是使已有的两个接口协同工作,而外观模式是提供一个更为方便的入口。
7. coding
7.1 被 适配者方法
package com.maidou.learning.design.structure.adapter.classadapter;
public class Adaptee {
public void adapteeRequest() {
System.out.println("被适配者");
}
}
7.2 接口
package com.maidou.learning.design.structure.adapter.classadapter;
public interface Target {
void request();
}
7.3 接口的实现
package com.maidou.learning.design.structure.adapter.classadapter;
public class ConcreteTarget implements Target{
@Override
public void request() {
System.out.println("被适配者");
}
}
7.4 适配者适配被适配者达到Target 目标
package com.maidou.learning.design.structure.adapter.classadapter;
public class Adapter extends Adaptee implements Target{
@Override
public 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();
}
}
7.5 类适配器的实现方式,其他的都不变
package com.maidou.learning.design.structure.adapt1er.objectadapter;
public class Adapter implements Target{
private Adaptee adaptee = new Adaptee();
@Override
public void request() {
adaptee.adapteeRequest();
}
}
8. 源码解析
8.1 XmlAdapter
8.1.1来源 javax.xml.bind.annotation.adapters
8.1.2coding
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;
8.1.3 说明
实行xml的序列化方法和反序列方法进行数据的传输
8.2 AdvisorAdapter
8.2.1 来源
org.springframework.aop.framework.adapter
8.2.2 coding
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);
}
}
8.3 JpaVendorAdapter
8.3.1 来源
org.springframework.orm.jpa
8.3.2 coding
public interface JpaVendorAdapter {
PersistenceProvider getPersistenceProvider();
@Nullable
default String getPersistenceProviderRootPackage() {
return null;
}
default Map<String, ?> getJpaPropertyMap(PersistenceUnitInfo pui) {
return this.getJpaPropertyMap();
}
default Map<String, ?> getJpaPropertyMap() {
return Collections.emptyMap();
}
@Nullable
default 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) {
}
}