美文网首页
第二部分·结构型模式-02-桥接模式(Bridge)

第二部分·结构型模式-02-桥接模式(Bridge)

作者: 玄德公笔记 | 来源:发表于2024-05-23 11:21 被阅读0次

1. 概念

桥(Bridge)使用组合关系将代码的实现层和抽象层分离,让实现层与抽象层代码可以分别自由变化。

例如 客户端调用桥接接口实现原有功能和扩展功能的组合

1.1 角色

  • Implementor(实施者):
    • 具体实施者的抽象,可以是一个接口。
  • Concrete Implementor(具体实施者):
    • 可以理解为扩展之前的原有功能
    • 桥接接口会在实现扩展功能的基础上调用它实现这些原有功能
  • Abstraction(抽象化):
    • 我们可以理解为桥接接口,它在提供扩展功能的同时也桥接了原有功能的接口
    • Refined Abstraction提供一个统一接口
    • 它关联了Implementor(或者进一步是Implementor的聚合)
  • Refined Abstraction(扩展抽象化):
    • 可以理解为它实现了具体的扩展功能,并实际调用了mplementor接口完成了原有功能

1.2 类图

image.png
classDiagram
  class Implementor  {
  <<interface>>
    +serviceImpl()ReturnType
  }
  class ConcreteImplementor {
    +serviceImpl()ReturnType
    +customService()Type
  }
  Implementor <|.. ConcreteImplementor
  
  class Abstraction  {
    <<interface>>
    +Implementor:Implementor 
    +abstractService():Data
  }

  class RefinedAbstraction{
    +extraService():Atype
  }
  Abstraction <|.. RefinedAbstraction

  Abstraction o..  Implementor 
  class Client{
  }

  Client --> Abstraction

2. 代码示例

2.1 设计

  • 定义一个实施者颜色
  • 定义三个具体实施者红色绿色黄色
    • 他们的use()方法来实现使用对应颜色
  • 定义一个抽象化类(桥接接口)笔刷
  • 定义两个扩展抽象化类粗笔刷细笔刷
    • 他们的画画方法
      • 实现扩展功能——用对应笔刷画画
      • 同时调用实施者接口,实现了对应的颜色功能
  • 定义一个工厂函数用来实例化一个具体的笔刷
  • 调用
    • 声明一个实施者
    • 实例化一个具体实施者
    • 用具体实施者实例化一个桥接
    • 调用桥接的方法实现原有功能和扩展功能的组合

2.1 代码

package main

import "fmt"

//定义实施者类
type Color interface {
    Use()
}

//定义具体实施者A
type Red struct{}

func (r Red) Use() {
    fmt.Println("Use Red color")
}
//定义具体实施者B
type Green struct{}

func (g Green) Use() {
    fmt.Println("Use Green color")
}
//定义具体实施者C
type Yellow struct{}

func (y Yellow) Use() {
    fmt.Println("Use Yellow color")
}

// 定义抽象化类(或叫桥接接口)
type BrushPen interface {
    DrawPicture()
}

// 定义扩展抽象化A
type BigBrushPen struct {
    Color
}

//提供扩展功能,同时选择原功能执行
func (bbp BigBrushPen) DrawPicture() {
    fmt.Println("Draw picture with big brush pen")
    bbp.Use()
}

// 定义扩展抽象化B
type SmallBrushPen struct {
    Color
}
//提供扩展功能,同时选择原功能执行
func (sbp SmallBrushPen) DrawPicture() {
    fmt.Println("Draw picture with small brush pen")
    sbp.Use()
}

// 定义工厂方法生产具体的扩展抽象化(此处为了方便展示,和桥接模式无关)
func NewBrushPen(t string, color Color) BrushPen {
    switch t {
    case "BIG":
        return BigBrushPen{
            Color: color,
        }
    case "SMALL":
        return SmallBrushPen{
            Color: color,
        }
    default:
        return nil
    }
}

func main() {
    //声明实施者
    var tColor Color
    fmt.Println("========== 第一次测试 ==========")
    //定义为具体实施者
    tColor = Red{}
    //用具体实施者实例化一个抽象化
    tBrushPen := NewBrushPen("BIG", tColor)
    //用抽象化的画画功能完成扩展功能(粗细笔刷)和对应原功能(颜色)的组合操作
    tBrushPen.DrawPicture()
    fmt.Println("========== 第二次测试 ==========")
    tColor = Green{}
    tBrushPen = NewBrushPen("SMALL", tColor)
    tBrushPen.DrawPicture()
    fmt.Println("========== 第三次测试 ==========")
    tColor = Yellow{}
    tBrushPen = NewBrushPen("BIG", tColor)
    tBrushPen.DrawPicture()
}
  • 输出
========== 第一次测试 ==========
Draw picture with big brush pen  
Use Red color
========== 第二次测试 ========== 
Draw picture with small brush pen
Use Green color
========== 第三次测试 ========== 
Draw picture with big brush pen  
Use Yellow color

2.2 类图

image.png
classDiagram
  class Color  {
  <<interface>>
    +Use()
  }
  class Red {
    +Use()
  }
  class Green{
    +Use()
  }
  class Yellow{
    +Use()
  }
  Color <|.. Red
  Color <|.. Green
  Color <|.. Yellow
  class BrushPen  {
    <<interface>>
    +DrawPicture()
  }

  class BigBrushPen{
    +Color
    +DrawPicture()
  }
  class SmallBrushPen{
    +Color
    +DrawPicture()
  }
  BrushPen <|.. BigBrushPen
  BrushPen <|.. SmallBrushPen

  BrushPen o..  Color
  class Client{
  }

  Client --> BrushPen

相关文章

  • 设计模式解析—桥接设计模式

    桥接模式(Bridge Pattern)定义和使用场景 定义桥接模式(Bridge Pattern):将抽象部分...

  • 结构型模式:桥接模式

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

  • 结构型模式3-桥接模式

    结构型模式3-桥接模式 桥接模式bridge 意图 将抽象部分与它的实现部分分离,使它们都可以独立的变化。 问题思...

  • 桥接模式

    介绍 桥接模式(Bridge Pattern) 也称为桥梁模式,是结构型设计模式之一。桥接模式的作用就是连接 "两...

  • Android设计模式——桥接模式(七大结构型)

    1.桥接模式介绍 桥接模式(Bridge Pattern)也称为桥梁模式,是七大结构型设计模式之一。 2....

  • 设计模式-桥接模式

    桥接模式介绍 桥接模式(Bridge Pattern)也称为桥梁模式,是结构型设计模式之一。顾名思义其与现实中的桥...

  • 桥接模式

    设计模式:桥接模式(Bridge)

  • 设计模式之桥接模式

    设计模式之桥接模式 Intro 桥接模式(Bridge),将抽象部分与它的实现部分分离,使得它们都可以独立地变化。...

  • 设计模式[10]-桥接模式-Bridge Pattern

    1. 桥接模式 桥接模式(Bridge Pattern)是结构性设计模式,可以处理多个维度的对象变化。如果系统中存...

  • 设计模式-桥接模式

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

网友评论

      本文标题:第二部分·结构型模式-02-桥接模式(Bridge)

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