博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TypeScript设计模式之工厂
阅读量:4467 次
发布时间:2019-06-08

本文共 5328 字,大约阅读时间需要 17 分钟。

看看用TypeScript怎样实现常见的设计模式,顺便复习一下。

学模式最重要的不是记UML,而是知道什么模式可以解决什么样的问题,在做项目时碰到问题可以想到用哪个模式可以解决,UML忘了可以查,思想记住就好。
这里尽量用原创的,实际中能碰到的例子来说明模式的特点和用处。

简单工厂模式 Simple Factory

特点:把同类型产品对象的创建集中到一起,通过工厂来创建,添加新产品时只需加到工厂里即可,也就是把变化封装起来,同时还可以隐藏产品细节。

用处:要new多个同一类型对象时可以考虑使用简单工厂。

注意:对象需要继承自同一个接口。

下面用TypeScript写一个枪工厂来看看简单工厂模式:

enum GunType{    AK,    M4A1,}interface Shootable{    shoot();}abstract class Gun implements Shootable{ // 抽象产品 - 枪    abstract shoot();}class AK47 extends Gun{ //具体产品 - AK47    shoot(){        console.log('ak47 shoot.');    }}class M4A1 extends Gun{ //具体产品 - M4A1    shoot(){        console.log('m4a1 shoot.');    }}class GunFactory{    static createGun(type: GunType): Gun{        switch(type){            case GunType.AK:                return new AK47();            case GunType.M4A1:                return new M4A1();            default:                throw Error('not support this gun yet');        }    }}GunFactory.createGun(GunType.AK).shoot();GunFactory.createGun(GunType.M4A1).shoot();//输出ak47 shoot.m4a1 shoot.

上面代码GunFactory工厂就是根据类型来创建不同的产品,使用的时候只需要引入这个工厂和接口即可。

这样就把变化封装到了工厂中,如果以后要支持狙击枪,只需要加个实现Gun接口的Sniper类就可以了。

工厂方法模式 Factory Method

特点:把工厂抽象出来,让子工厂来决定怎么生产产品, 每个产品都由自己的工厂生产。

用处:当产品对象需要进行不同的加工时可以考虑工厂方法。

注意:这不是所谓的简单工厂的升级版,两者有不同的应用场景。

继续用TypeScript写一个枪工厂来看看工厂方法模式:

interface Shootable{    shoot();}abstract class Gun implements Shootable{ // 抽象产品 - 枪    abstract shoot();}class AK47 extends Gun{ //具体产品 - AK47    shoot(){        console.log('ak47 shoot.');    }}class M4A1 extends Gun{ //具体产品 - M4A1    shoot(){        console.log('m4a1 shoot.');    }}abstract class GunFactory{ //抽象枪工厂    abstract create(): Gun;}class AK47Factory extends GunFactory{ //Ak47工厂    create(): Gun{        let gun = new AK47();  // 生产Ak47        console.log('produce ak47 gun.');        this.clean(gun);       // 清理工作        this.applyTungOil(gun);// Ak47是木头枪托,涂上桐油        return gun;    }    private clean(gun: Gun){        //清洗        console.log('clean gun.');    }    private applyTungOil(gun: Gun){        //涂上桐油        console.log('apply tung oil.');    }}class M4A1Factory extends GunFactory{ //M4A1工厂    create(): Gun{        let gun = new M4A1();   // 生产M4A1        console.log('produce m4a1 gun.');        this.clean(gun);        // 清理工作        this.sprayPaint(gun);   // M4是全金属,喷上漆        return gun;    }    private clean(gun: Gun){        //清洗        console.log('clean gun.');    }    private sprayPaint(gun: Gun){        //喷漆        console.log('spray paint.');    }}let ak47 = new AK47Factory().create();ak47.shoot();let m4a1 = new M4A1Factory().create();m4a1.shoot();//outputproduce ak47 gun.clean gun.apply tung oil.ak47 shoot.produce m4a1 gun.clean gun.spray paint.m4a1 shoot.

可以看到Ak47和M4A1在生产出来后的处理不一样,Ak需要涂桐油,M4需要喷漆,用简单工厂就比较难做到,所以就每个产品都弄个工厂来封装各自己的生产过程。

另外的好处是当加入其他枪比如沙漠之鹰时,再加一个产品和产品工厂就好了,并不需要改变现有代码,算是做到了遵守开闭原则。
缺点也明显,增加一个产品就需要多加两个类,增加了代码复杂性。

抽象工厂模式 Abstract Factory

特点:同样隐藏了具体产品的生产,不过生产的是多种类产品。

用处:当需要生产的是一个产品族,并且产品之间或多或少有关联时可以考虑抽象工厂方法。

注意:和工厂方法的区别,工厂方法是一个产品, 而抽象工厂是产品族,线和面的区别。

继续用枪,外加子弹,用TypeScript写一个抽象枪工厂来看看抽象工厂模式:

interface Shootable{    shoot();}abstract class Gun implements Shootable{ // 抽象产品 - 枪    private _bullet: Bullet;    addBullet(bullet: Bullet){        this._bullet = bullet;    }    abstract shoot();}class AK47 extends Gun{ //具体产品 - AK47    shoot(){        console.log(`ak47 shoot with ${this._bullet}.`);    }}class M4A1 extends Gun{ //具体产品 - M4A1        shoot(){        console.log(`m4a1 shoot with ${this._bullet}.`);    }}abstract class Bullet{ // 抽象子弹    abstract name: string;}class AkBullet{ // AK 子弹    name: string = 'ak bullet';}class M4Bullet{ // m4a1 子弹    name: string = 'm4a1 bullet';}abstract class ArmFactory{ //抽象军工厂    abstract createGun(): Gun;    abstract createBullet(): Bullet;}class AK47Factory extends ArmFactory{    createGun(): Gun{        let gun = new AK47();  // 生产Ak47        console.log('produce ak47 gun.');        this.clean(gun);       // 清理工作        this.applyTungOil(gun);// Ak47是木头枪托,涂上桐油        return gun;    }    private clean(gun: Gun){        //清洗        console.log('clean gun.');    }    private applyTungOil(gun: Gun){        //涂上桐油        console.log('apply tung oil.');    }    createBullet(): Bullet{        return new AkBullet();    }}class M4A1Factory extends ArmFactory{ //M4A1工厂    createGun(): Gun{        let gun = new M4A1();   // 生产M4A1        console.log('produce m4a1 gun.');        this.clean(gun);        // 清理工作        this.sprayPaint(gun);   // M4是全金属,喷上漆        return gun;    }    private clean(gun: Gun){        //清洗        console.log('clean gun.');    }    private sprayPaint(gun: Gun){        //喷漆        console.log('spray paint.');    }    createBullet(): Bullet{        return new M4Bullet();    }}//使用function shoot(gun: Gun, bullet: Bullet) // 使用生产的枪和子弹{    gun.addBullet(bullet);    gun.shoot();}let akFactory = new AK47Factory();shoot(akFactory.createGun(), akFactory.createBullet());let m4a1Factory = new M4A1Factory();shoot(m4a1Factory.createGun(), m4a1Factory.createBullet());//输出produce ak47 gun.clean gun.apply tung oil.add bullet: ak bulletak47 shoot with ak bullet.produce m4a1 gun.clean gun.spray paint.add bullet: m4a1 bulletm4a1 shoot with m4a1 bullet.

工厂除了生产枪外还生产子弹,子弹和枪算是一个产品族,使用者接触到的只有抽象工厂和抽象产品,隐藏了具体实现细节。

在大的框架下面有很多小项目时用抽象工厂配合如动态对象生成之类的技术就可以很容易实现灵活的架构。

转载于:https://www.cnblogs.com/brookshi/p/6505608.html

你可能感兴趣的文章
Windows Store App之数据存储
查看>>
English class 82 The Importance of traveling
查看>>
python用递归函数解汉诺塔游戏
查看>>
Redis与Python交互
查看>>
Maximum-SubsequenceSum
查看>>
常用的一些shell变量
查看>>
Android无法删除项目+导入项目报错
查看>>
poj 2349(最小生成树应用)
查看>>
Shell编程-条件测试 | 基础篇
查看>>
AngularJs学习笔记1——总体介绍
查看>>
C语言第十二讲,文件操作.
查看>>
绝对定位和相对定位
查看>>
实习第二天——学习mac终端命令(unix命令)和git代码管理
查看>>
微信支付
查看>>
吴裕雄--天生自然 高等数学学习:含参变量的积分
查看>>
成本的费用归集
查看>>
本周ASP.NET英文技术文章推荐[01/28 - 02/03]
查看>>
运行时库组件 RuntimePack v19.06.05 Full 纯净安装版
查看>>
NYOJ100 - 1的个数
查看>>
左侧定宽右侧自适应布局
查看>>