美文网首页程序员
Typescript 桥接模式

Typescript 桥接模式

作者: 我不叫奇奇 | 来源:发表于2018-11-08 20:34 被阅读38次

标签: 前端 设计模式 桥接模式 typescript bridge


如果下面的代码你能轻易阅读,那么你已经熟悉桥接模式,可以接着学习其他的设计模式。

bridge.jpg

桥接模式:桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interfce)模式。

实际场景

我们在现实中描述物体通常是多个维度的。例如我们描述衣服,会描述白色的短袖、黑色的毛衣、黄色的长袖...
转换成代码,他们的组合种类繁多。
通过观察,我们发现其中主要有两种类别。

  • 衣服的类型(短袖/长袖/毛衣/...)
  • 颜色(白/黑/红/黄/...)

将两种类别抽象出来,再由其自由组合,这样我们代码的可维护性和拓展性就会大大增强。
这就引出了桥接模式(Bridge)

桥接模式结构

桥接模式主要包含如下几个角色:
Abstraction:抽象类。
RefinedAbstraction:扩充抽象类。
Implementor:实现类接口。
ConcreteImplementor:具体实现类 。

桥接模式的例子

  • 类型和颜色的枚举
/* clothes-type-enum.ts */
enum ClothesTypeEnum {
    Vest,
    Shirt,
    Jacket,
    Hoodies,
    Sweater,
}
export default ClothesTypeEnum

/* color-enum.ts */
enum ColorEnum {
    White,
    Red,
    Yellow,
    Black
}
export default ColorEnum
  • 类型的抽象类
/* abstract-clothes-class.ts */
import ColorInterface from './color-interface';

export default abstract class AbstractClothesClass {
    protected color: ColorInterface;

    public setColor(color: ColorInterface) {
        this.color = color;
    }

    public abstract getClothes: Function;
}
  • 类型的扩充抽象类
/* jacket-producer.ts */
import AbstractClothesClass from '../abstract-clothes-class';

export default class JacketProducer extends AbstractClothesClass {
    public getClothes() {
        let color = this.color.getColor();
        console.log(color + 'jacket');
    }
}

/* shirt-producer.ts */
import AbstractClothesClass from '../abstract-clothes-class';

export default class ShirtProducer extends AbstractClothesClass {
    public getClothes() {
        let color = this.color.getColor();
        console.log(color + 'shirt');
    }
}
  • 颜色的接口
/* color-interface.ts */
export default interface ColorInterface {
    getColor: () => {}
}
  • 颜色的实现类
/* black-color.ts */
import ColorInterface from '../color-interface';

export default class BlackColor implements ColorInterface {
    public getColor() {
        return 'black';
    }
}

/* white-color.ts */
import ColorInterface from '../color-interface';

export default class WhiteColor implements ColorInterface {
    public getColor() {
        return 'white';
    }
}
  • 桥接具体实现类
/* bridge.ts */
import AbstractClothesClass from './abstract-clothes-class';
import JacketProducer from './clothes-producers/jacket-producer';
import ShirtProducer from './clothes-producers/shirt-producer';
import ClothesTypeEnum from './clothes-type-enum';
import ColorEnum from './color-enum';
import BlackColor from './color-types/black-color';
import WhiteColor from './color-types/white-color';

export default class CloseShop {
    private static _instance: CloseShop;

    public static get instance() {
        if (!this._instance) {
            this._instance = new CloseShop();
        }
        return this._instance;
    }

    public getClothes(type: ClothesTypeEnum, color: ColorEnum) {
        let clothes: AbstractClothesClass;
        switch (type) {
            case ClothesTypeEnum.Jacket:
                clothes = new JacketProducer();
                break;
            case ClothesTypeEnum.Shirt:
                clothes = new ShirtProducer();
                break;
            default:
                throw new Error('not support type' + type)
        }
        switch (color) {
            case ColorEnum.White:
                clothes.setColor(new WhiteColor());
                break;
            case ColorEnum.Black:
                clothes = new ShirtProducer();
                clothes.setColor(new BlackColor());
                break;
            default:
                throw new Error('not support color' + color)
        }
    }
}
  • 产品调用
CloseShop.instance.getClothes(ClothesTypeEnum.Shirt, ColorEnum.Black);

桥接模式的利弊

利:
桥接模式把抽象和实现隔离开,有助于独立的管理软件的各个部分,bug也更容易查找,发生严重故障的可能性也减少了。
弊:
在我们看来,桥接模式没有什么真正的缺点,他会把抽象和实现解耦,提高系统的模块化程度。但是注意不要对该模式滥用而引发过度设计。

相关文章

  • Typescript 桥接模式

    标签: 前端 设计模式 桥接模式 typescript bridge 如果下面的代码你能轻易阅读,那么你已经熟悉桥...

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interface)模式或...

  • 结构型模式:桥接模式

    文章首发:结构型模式:桥接模式 七大结构型模式之二:桥接模式。 简介 姓名 :桥接模式 英文名 :Bridge P...

  • 设计模式之桥接模式

    设计模式之桥接模式 1. 模式定义 桥接模式又称柄体模式或接口模式,它是一种结构性模式。桥接模式将抽象部分与实现部...

  • 06-01-001 虚拟机的网络连接方式(转运整理)

    一、Bridged(桥接模式) 什么是桥接模式?桥接模式就是将主机网卡与虚拟机虚拟的网卡利用虚拟网桥进行通信。在桥...

  • 桥接模式与中介模式

    桥接模式-BRIDGE 对桥接模式感兴趣,是因为公司业务上需要桥接Html5和ReactNative两个平台。桥接...

  • 设计模式——桥接模式

    设计模式——桥接模式 最近公司组件分享设计模式,然而分配给我的是桥接模式。就在这里记录我对桥接模式的理解吧。 定义...

  • 桥接模式

    个人博客http://www.milovetingting.cn 桥接模式 模式介绍 桥接模式也称为桥梁模式,是结...

  • 桥接模式

    桥接模式 参考原文: https://zhuanlan.zhihu.com/p/62390221 定义 桥接模式 ...

  • 10-桥接模式

    桥接模式-Bridge Pattern【学习难度:★★★☆☆,使用频率:★★★☆☆】 处理多维度变化——桥接模式(...

网友评论

    本文标题:Typescript 桥接模式

    本文链接:https://www.haomeiwen.com/subject/tergxqtx.html