适配器模式

一. 适配器模式

  当一个接口中有多个方法而我们只需要使用其中某个特定的方法时,这时就创建一个中间类(抽象类)去实现接口中的所有方法,然后创建中间类的子类去继承中间类,从而获取需要的特定方法

二. 例子

1. 接口

public interface TestManager {

    void test();

    String test2();

    int test3();


}

2. 中间类

abstract class ATestManagerImpl implements TestManager {

    @Override
    public void test() {

    }

    @Override
    public int test3() {
        return 0;
    }


    public String test2() {
        return "";
    }



}

3. 实现类

public class TestManagerImpl extends ATestManagerImpl {

    @Override
    public String test2() {
        String result = "123";
        return result;
    }



}
原文地址:https://www.cnblogs.com/jingjiren/p/13132013.html