java设计模式——适配器模式

适配器模式

1.定义:

适配器模式把一个类的接口转换成客户所期待的另一种接口,从而使原本因接口不匹配而无法工作在一起的两个类能够在一起工作。

2.使用场景:

  • 系统需要使用现有的类,而此类的接口不符合系统的需求,即接口不兼容;
  • 想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作;
  • 需要一个统一的输出接口,而输入端的类型不可预知。

3.UML图

4.详解:

适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。
首先,有一个目标类与期待接口:

public static class Source {
        public void originalMethod() {
            System.out.println("this is original method");
        }
    }

    public interface Targetable {
        void originalMethod();

        void newMethod();
    }
4.1类适配器模式

当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。

/**
     * 类适配器模式:类适配器模式主要是适配器类要继承Source类,并实现拓展接口(要求拓展接口中必须要有与Source类一样的method,并拓展了newMethod)
     */
    public static class SourceAdapter extends Source implements Targetable {
        @Override
        public void newMethod() {
            System.out.println("this is class adapter new method");
        }
    }
4.2对象适配器模式

当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。

/**
     * 对象适配器模式:与类适配器模式一样,不同的是,对象适配器模式不再试继承Source类实现Targetable接口了,而是持有Source的实例,并让Source实例去调用原有method
     */
    public static class Wrapper implements Targetable {
        private Source source;

        public Wrapper(Source source) {
            this.source = source;
        }

        @Override
        public void originalMethod() {
            source.originalMethod();
        }

        @Override
        public void newMethod() {
            System.out.println("this is target adapter new method");
        }
    }
4.3接口适配器模式

当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

/**
     * 接口适配器模式:主要是为了不必要的接口方法,引入额外的类,该类实现接口,并全部重新接口方法;然后目标类只需要继承该类,重写需要的方法即可
     */
    public interface Sourceable {
        void method1();

        void method2();
    }

    public static abstract class SourceableWrapper implements Sourceable {
        @Override
        public void method1() {
        }

        @Override
        public void method2() {
        }
    }

    public static class Stub1 extends SourceableWrapper {
        @Override
        public void method1() {
            super.method1();
            System.out.println("this is interface adapter first method");
        }
    }

    public static class Stub2 extends SourceableWrapper {
        @Override
        public void method2() {
            super.method2();
            System.out.println("this is interface adapter second method");
        }
    }

测试代码:

public static void main(String[] args) {
        Targetable targetable = new SourceAdapter();
        targetable.originalMethod();
        targetable.newMethod();

        System.out.println("==============below is target adapter method================");
        Source source = new Source();
        Targetable wrapper = new Wrapper(source);
        wrapper.originalMethod();
        wrapper.newMethod();

        System.out.println("==============below is interface adapter method================");
        Sourceable sourceable1 = new Stub1();
        sourceable1.method1();
        sourceable1.method2();//void

        Sourceable sourceable2 = new Stub2();
        sourceable2.method1();//void
        sourceable2.method2();
      /**输出结果:
         this is original method
         this is class adapter new method
         ==============below is target adapter method================
         this is original method
         this is target adapter new method
         ==============below is interface adapter method================
         this is interface adapter first method
         this is interface adapter second method
         */
    }

5.代码托管地址

适配器模式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容