Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设计模式之适配器模式 #36

Open
zgq105 opened this issue May 13, 2019 · 0 comments
Open

设计模式之适配器模式 #36

zgq105 opened this issue May 13, 2019 · 0 comments

Comments

@zgq105
Copy link
Owner

zgq105 commented May 13, 2019

image

1. 什么是适配器模式?有什么作用?

适配器模式是指两个互不相关的类或者接口通过适配器进行关联的模式,属于结构型设计模式。
其作用就是让两个互不相关的类或者接口能够互相通信,启到桥梁的作用。主要有类适配器模式、类对象适配器模式和接口对象适配器模式。
image

2. 如何实现?

2.1 类适配器模式

//待适配的类

public class Source {
    public void f1(){
        System.out.println("Source-f1");
    }
}

//目标接口

public interface Target {
    void f1(); //注意这里的方法签名和待适配类中方法一致
    void f2();
}

//适配器(Adapter)类

public class Adapter extends Source implements Target {

    @Override
    public void f2() {
        System.out.println("Adapter-f2");
    }
}

//调用

Target target = new Adapter();
target.f1();
target.f2();

关键点:

  • 适配器类(Adapter)继承待适配类(Source )同时实现目标接口(Target ),待适配类(Source )中的需要暴露给外部调用的方法和目标接口(Target )中的方法名一致,本质就是待适配类实现了目标接口中的方法;从而实现了在Target中访问Source中的方法。

2.2 对象适配器模式

//待适配接口

public interface Source3 {

    void f1();
    void f2();
}

//待适配接口实现类

public class Source3Impl implements Source3 {
    @Override
    public void f1() {
        System.out.println("f1");
    }

    @Override
    public void f2() {
        System.out.println("f2");
    }
}

//目标接口

public interface Target3 {
    void f1();
    void f2();
}

//适配器(Adapter)类


//调用

Source2 source2 = new Source2();
Target2 target2 = new Adapter2(source2);
target2.f1();
target2.f2();

关键点:

  • 适配器类(Adapter2)实现目标接口(Target2 ),同时持有待适配类对象的引用。

2.3 接口对象适配器模式

//待适配的类

public class Source2 {

    public void f1(){
        System.out.println("Source2-f1");
    }
}

//目标接口

public interface Target2 {
    void f1();
    void f2();
}

//适配器(Adapter)类

public class Adapter3 implements Target3 {

    private Source3 mSource3;

    public Adapter3(Source3 source3) {
        this.mSource3 = source3;
    }

    @Override
    public void f1() {
        mSource3.f1();
    }

    @Override
    public void f2() {
        mSource3.f2();
    }
}

//调用

Source3 source3 = new Source3Impl();
Target3 target3 = new Adapter3(source3);
target3.f1();
target3.f2();

关键点:

  • 适配器类(Adapter3)实现目标接口(Target3 ),同时持有待适配接口(Source3 )对象的引用。

3. JDK或Android的应用举例

在java JDK中InputStreamReader和OutputStreamWriter;在Android SDK中ArrayAdapter、BaseAdapter、ListAdapter等都使用了大量的适配器模式。

4. 小结

适配器模式使得两个互不相干的类或接口能进行通信;适配器模式主要有三个要素,确定待适配的对象、目标对象、适配器对象(关联桥梁类)。适配器模式只适用一方访问另一方的情形,即是单向的。

@zgq105 zgq105 changed the title 设计模式之适配器模式总结 设计模式之适配器模式 May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant