设计模式 -- 适配器模式(Adapter Pattern)

适配器模式可以将一个类的接口,转换成客户端期望的另一个接口,让两个原本不兼容的接口可以无缝对接。

别名:Wrapper(包装器)

ListView需要ListAdapter的getCount(),getItem(),getView()等方法,为了兼容List<T>,Cursor等数据类型作为数据源,专门定义两个适配器来适配他们:ArrayAdapter和CursorAdapter。

//目标接口
interface Target
{
    public void request();
}
//适配者接口
class Adaptee
{
    public void specificRequest()
    {       
        System.out.println("适配者中的业务代码被调用!");
    }
}



//类适配器类
class ClassAdapter extends Adaptee implements Target
{
    public void request()
    {
        specificRequest();
    }
}
//客户端代码
public class ClassAdapterTest
{
    public static void main(String[] args)
    {
        System.out.println("类适配器模式测试:");
        Target target = new ClassAdapter();
        target.request();
    }
}



//对象适配器类
class ObjectAdapter implements Target
{
    private Adaptee adaptee;
    public ObjectAdapter(Adaptee adaptee)
    {
        this.adaptee=adaptee;
    }
    public void request()
    {
        adaptee.specificRequest();
    }
}
//客户端代码
public class ObjectAdapterTest
{
    public static void main(String[] args)
    {
        System.out.println("对象适配器模式测试:");
        Adaptee adaptee = new Adaptee();
        Target target = new ObjectAdapter(adaptee);
        target.request();
    }
}



// 接口适配器
// 目标接口
public interface LogFactory {     
    void debug(String tag,String message); 
}


// 源接口
public interface NbLogger {
    void d(int priority, String message, Object ... obj);
}
//具体提供日志功能的实现类
public class NbLoggerImp implements NbLogger {
    @Override
    public void d(int priority, String message, Object... obj) {
        System.out.println(String.format("牛逼logger记录:%s",message));
    }
}


public class LogAdapter implements LogFactory {
    private NbLogger nbLogger;


    public LogAdapter(NbLogger nbLogger) {
        this.nbLogger = nbLogger;
    }


    @Override
    public void debug(String tag, String message) {
        Objects.requireNonNull(nbLogger);
        nbLogger.d(1, message);
    }
}


public class AdapterClient {
    public void recordLog() {
        LogFactory logFactory = new LogAdapter(new NbLoggerImp());
        logFactory.debug("Test", "我将使用牛逼logger打印log");
    }
}
public interface Sourceable {
	public void method1();
	public void method2();
}


public abstract class Wrapper2 implements Sourceable{
	public void method1(){}
	public void method2(){}
}


public class SourceSub1 extends Wrapper2 {
	public void method1(){
		System.out.println("the sourceable interfaces first Sub1!");
	}
}


public class SourceSub2 extends Wrapper2 {
	public void method2(){
		System.out.println("the sourceable interfaces second Sub2!");
	}
}


public class WrapperTest {
	public static void main(String[] args) {
		Sourceable source1 = new SourceSub1();
		Sourceable source2 = new SourceSub2();
		
		source1.method1();
		source1.method2();
		source2.method1();
		source2.method2();
	}
}

根据以下文章总结:

  1. Java设计模式:23种设计模式全面解析(超级详细)HYPERLINK
  2. 3种设计模式详解
  3. Android系统编程思想:设计模式
  4. 设计模式
  5. java设计模式
  6. 设计模式
  7. 设计模式 在源码中的应用
  8. Android系统设计中存在设计模式分析 https://
  9. Android设计模式系列 - 基于android的各种代码分析各种设计模式
经验分享 程序员 微信小程序 职场和发展