美文网首页
第十七章:桥接模式

第十七章:桥接模式

作者: Benedict清水 | 来源:发表于2022-07-23 08:17 被阅读0次

一、从场景中领悟什么是桥接模式

在几何图形的分类中,假设我们有矩形和椭圆之分,这时我们又希望加入颜色(红色、绿色)来拓展它的层级。

用程序来模拟
(1)python语言版

from abc import ABCMeta, abstractmethod


class Shape(metaclass=ABCMeta):
    """形状"""

    def __init__(self, color):
        self._color = color

    def getShapeType(self):
        pass

    def getShapeInfo(self):
        return self._color.getColor() + "的" + self.getShapeType()


class Rectangle(Shape):
    """矩形"""

    def __init__(self, color):
        super().__init__(color)

    def getShapeType(self):
        return "矩形"


class Ellipse(Shape):
    """椭圆"""

    def __init__(self, color):
        super().__init__(color)

    def getShapeType(self):
        return "椭圆"


class Color(metaclass=ABCMeta):
    """颜色"""

    @abstractmethod
    def getColor(self):
        pass


class Red(Color):
    """红色"""

    def getColor(self):
        return "红色"


class Green(Color):
    """绿色"""

    def getColor(self):
        return "绿色"


def testShap():
    redRect = Rectangle(Red())
    print(redRect.getShapeInfo())
    greenRect = Rectangle(Green())
    print(greenRect.getShapeInfo())

    redEllipse = Ellipse(Red())
    print(redEllipse.getShapeInfo())
    greenEllipse = Ellipse(Green())
    print(greenEllipse.getShapeInfo())


if __name__ == "__main__":
    testShap()

(2)go语言版

package main

import "fmt"

type Color interface {
    getColor() string
}

type Red struct {
}

func (r Red) getColor() string {
    return "红色"
}

type Green struct {
}

func (g Green) getColor() string {
    return "绿色"
}

type Shape interface {
    getShapeType() string
    getShapeInfo() string
}

type Rectangle struct {
    color Color
}

func (r Rectangle) getShapeType() string {
    return "矩形"
}

func (r Rectangle) getShapeInfo() string {
    return r.color.getColor() + "的" + r.getShapeType()
}

type Ellipse struct {
    color Color
}

func (e Ellipse) getShapeType() string {
    return "椭圆"
}

func (e Ellipse) getShapeInfo() string {
    return e.color.getColor() + "的" + e.getShapeType()
}

func testShape() {
    readRect := Rectangle{Red{}}
    fmt.Println(readRect.getShapeInfo())
    greenRect := Rectangle{Green{}}
    fmt.Println(greenRect.getShapeInfo())

    readEllipse := Ellipse{Red{}}
    fmt.Println(readEllipse.getShapeInfo())
    greenEllipse := Ellipse{Green{}}
    fmt.Println(greenEllipse.getShapeInfo())
}

func main() {
    testShape()
}

二、什么是桥接模式

将抽象和实现解耦,使得它们可以独立地变化。桥接模式也被称为桥梁模式。桥梁模式是结构型模式,侧重于软件结构。而策略模式关注的是对算法、规则的封装,使得算法可以独立于使用它的用户而变化;策略模式是行为型模式,侧重于对象行为。

三、桥接模式的模型抽象

桥接模式的类图.png

Implementor是一个实现化角色,定义必要的行为和属性;ImplementorImplA和ImplementorImplB是具体的实现化角色。Abstraction是抽象化角色,它的作用是对实现化角色 Implementor 进行一些行为的抽象;RefinedAbstraction 是抽象化角色的具体实现类,对抽象化角色进行修改。

对于场景中的问题,如果我们来用类图来解决就会变成如下的类图 继承关系的类图.png 如果我们再增加几个形状(如三角形),再增加几种颜色(如蓝色、紫色),这个类图将会越来越臃肿。这时,我们就希望对这个设计进行解耦,将形状和颜色分成两个分支,独立发展,互不影响。桥接模式就派上用场了,我们看一下使用桥接模式后的类图,如图下图所示。 使用桥接模式的类图.png

四、应用场景

(1)一个产品(或对象)有多种分类和多种组合,即两个(或多个)独立变化的维度,每个维度都希望独立进行扩展。
(2)因为使用继承或因为多层继承导致系统类的个数急剧增加的系统,可以改用桥接模式来实现。

摘录来自
人人都懂设计模式:从生活中领悟设计模式:Python实现

相关文章

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(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【学习难度:★★★☆☆,使用频率:★★★☆☆】 处理多维度变化——桥接模式(...

  • Java设计模式——桥接模式

    Java设计模式之桥接模式 回顾 上一期分享了适配器模式,主要为了实现解耦 桥接模式 简介 桥接模式是对象的结构模...

网友评论

      本文标题:第十七章:桥接模式

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