zoukankan      html  css  js  c++  java
  • Typescript 适配器模式(Adapter)

    请仔细阅读下面代码,理解其中的设计理念。

     
    adapter.jpg

    适配器模式

    适配器模式: 一般是指不兼容的方法调用之间的一个代码薄层。
    分为类适配器和对象适配器,但是类适配器一般要多继承,所以不推荐使用。

    实际场景

    我们在旧的系统中实现一个需要传入三个参数的方法,并且已经有很多地方在使用这个方法。现在有一个新的系统也想使用这个方法,但是它只能提供将三个参数合并为一个数组的参数。
    这时我们需要一个代码薄层将数据进行转化,使两个系统完成对接。

    适配器模式的结构

    • 原有的系统的接口
    • 适配器类实现原系统接口

    适配器模式的例子

    现在B系统需要实现一个输出时分秒的方法,提供的参数是一个对象(如下)

    {
        hours: string, 
        minutes: string,
        seconds: string
    }
    

    A系统中已经实现一个输出三个变量的方法,需要提供的参数是三个字符串。

    /* system-a.ts */
    export default class SystemA {
        public static instance = new SystemA();
    
        public echoABC(a: string, b: string, c: string) {
            console.log(a, b, c);
        }
    }
    

    现在我们需要一个adapter来让B系统调用A系统的方法去输出事件。

    /* i-system-b.ts */
    export default interface ISystemB {
        echoTime: (args: {hours: string, minutes: string, seconds: string}) => any;
    }
    
    /* system-b-to-a-adapter.ts */
    import ISystemB from './i-system-b';
    import SystemA from './system-a';
    
    export default class SystemBToAAdapter implements ISystemB{
        public static instance: SystemBToAAdapter = new SystemBToAAdapter();
    
        public echoTime(args: {hours: string, minutes: string, seconds: string}) {
            // 将我们的参数稍微改动一下就可以调用A系统的接口而不用重写
            SystemA.instance.echoABC(args.hours, args.minutes, args.seconds);
        }
    }
    
    

    B系统的调用

    /* system-b.ts */
    import ISystemB from './i-system-b';
    import SystemBToAAdapter from './system-b-to-a-adapter';
    
    export default class SystemB implements ISystemB{
        public static instance = new SystemB();
    
        public echoTime(args: {hours: string, minutes: string, seconds: string}) {
            SystemBToAAdapter.instance.echoTime(args);
        }
    }
    

    适配器模式和桥接模式的区别

    适配器模式一般用于系统间的适配。
    桥接模式一般用于方法之间的连接和扩充。

    适配器模式的利弊

    利:有助于避免大规模改写现有的客户代码。
    弊:如果代码本身不合理,与其之后对代码写一个又一个适配器,还不如直接重构现有代码而一劳永逸。



    作者:我不叫奇奇
    链接:https://www.jianshu.com/p/e0bda9224d9e
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    PIE SDK介绍
    PIE软件介绍
    PIE SDK与Python结合说明文档
    转载博客(Django2.0集成xadmin管理后台遇到的错误)
    python+django学习二
    python+django学习一
    HTML练习二--动态加载轮播图片
    HTML练习一
    LeetCode 999. 车的可用捕获量
    LeetCode 892. 三维形体的表面积
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/14103786.html
Copyright © 2011-2022 走看看