zoukankan      html  css  js  c++  java
  • java设计模式--适配器模式

    定义

      将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类一起工作。适配器模式分为类结构模型模式和对象结构型模式两种,前者模式中类直接的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少。

    优点

      客户端通过适配器可以透明的调用目标接口

      复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。

      将目标类和适配类解耦,解决了目标类和适配类接口不一致的问题。

    缺点

      对类适配器来说,更换适配器的过程比较复杂

    结构

      类适配器模式可以采用多重继承方式实现。java不支持多继承,但可以定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件。

      对象适配器模式可以将现有组件库中已经实现的组件引入适配器类中,该类同时实现当前系统的业务接口。

      适配器模式包含以下主要角色

        目标接口(Target):当前系统业务所期待的接口,它可以是抽象类或接口

        适配者类(Adaptee):它是被访问和适配的现存组件库中的组件接口

        适配器类(Adapter):它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者

    类适配器模式代码

    package adapter;
    //目标接口
    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();
        }
    }

    对象适配器模式的代码

    package adapter;
    //对象适配器类
    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();
        }
    }

    应用场景

      以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致

      使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同

    模式扩展

    适配器模式(Adapter)可扩展为双向适配器模式,双向适配器类既可以把适配者接口转换成目标接口,也可以把目标接口转换成适配者接口

    package adapter;
    //目标接口
    interface TwoWayTarget
    {
        public void request();
    }
    //适配者接口
    interface TwoWayAdaptee
    {
        public void specificRequest();
    }
    //目标实现
    class TargetRealize implements TwoWayTarget
    {
        public void request()
        {       
            System.out.println("目标代码被调用!");
        }
    }
    //适配者实现
    class AdapteeRealize implements TwoWayAdaptee
    {
        public void specificRequest()
        {       
            System.out.println("适配者代码被调用!");
        }
    }
    //双向适配器
    class TwoWayAdapter  implements TwoWayTarget,TwoWayAdaptee
    {
        private TwoWayTarget target;
        private TwoWayAdaptee adaptee;
        public TwoWayAdapter(TwoWayTarget target)
        {
            this.target=target;
        }
        public TwoWayAdapter(TwoWayAdaptee adaptee)
        {
            this.adaptee=adaptee;
        }
        public void request()
        {
            adaptee.specificRequest();
        }
        public void specificRequest()
        {       
            target.request();
        }
    }
    //客户端代码
    public class TwoWayAdapterTest
    {
        public static void main(String[] args)
        {
            System.out.println("目标通过双向适配器访问适配者:");
            TwoWayAdaptee adaptee=new AdapteeRealize();
            TwoWayTarget target=new TwoWayAdapter(adaptee);
            target.request();
            System.out.println("-------------------");
            System.out.println("适配者通过双向适配器访问目标:");
            target=new TargetRealize();
            adaptee=new TwoWayAdapter(target);
            adaptee.specificRequest();
        }
    }
  • 相关阅读:
    转载: Ubuntu 在命令下,安装中文环境的方法。
    java复制文件范例代码
    Cesium-entiy闪烁范例
    转载:贝塞尔曲线计算公式
    转载: utm坐标和经纬度相互转换
    arcgis 地图如何转到supermap平台
    Linux查看修改文件句柄数
    转载:Linux目录文件的权限查看与修改
    欧拉系统-登陆 SSH 出现 Access Denied 错误
    关于 nodejs sequelize 事务批量拆分
  • 原文地址:https://www.cnblogs.com/javadongx/p/shejimoshi_adapter.html
Copyright © 2011-2022 走看看