TypeScript适配器模式

// 适配器模式 定义一个客户端所期待的接口,让原来接口不匹配的两个类能在一起工作

interface AA {

    doA: () => string;

}

interface BB {

    doB: () => string;

}

interface CC {

    doC: () => string;

}

扫描二维码关注公众号,回复: 11813492 查看本文章

class aaa implements AA {

    doA(): string {

        return "AA";

    }

}

class bbb implements BB {

    doB(): string {

        return "BB";

    }

}

//具体的构件

class cccc implements CC {

    doC(): string {

        return "CC";

    }

}

class shiyongzhe {

    private aa: AA = null;

    private bb: BB = null;

    private cc: CC = null;

    private a : string  = null ;

    private b : string  = null ;

    private c : string  = null ; 

    constructor(aa: AA, bb: BB, cc: CC) {

        this.aa = aa;

        this.bb = bb;

        this.cc = cc;

        this.a =this.aa.doA();

        this.b =this.bb.doB();

        this.c =this.cc.doC();

    }

    getInfo1(): string {

        return this.a;

    }

    getInfo2(): string {

        return this.b;

    }

}

let aa = new aaa();

let bb = new bbb();

let ccc = new cccc();

let cobj:shiyongzhe = new shiyongzhe(aa, bb, ccc);

YBLog.log( "Test",cobj.getInfo1() );

//优点: 没关联的类可以混在一次,只要适配这个角色就可以了。 增加类的透明,提高类的复用,灵活。

//缺点: 解决的是 服役中 的项目问题。

猜你喜欢

转载自blog.csdn.net/ting100/article/details/108739879