桥接模式(Bridge)

参考:

桥接设计模式 (refactoringguru.cn)

桥接模式(Bridge模式)详解 (biancheng.net)

design-patterns-cpp/bridge at master · JakubVojvoda/design-patterns-cpp · GitHub

一、什么是桥接模式?

定义:将抽象与实现分离,用组合关系代替继承,从不同的维度进行扩展。

简单来说,就是A类中包含有B类接口,通过构造函数传递B类的实现,这个B类就是桥。

比如模型玩具有很多种,红色的球体、蓝色的球体、红色的正方体、蓝色的正方体等。可以按形状、按颜色等两个维度的进行划分。A类表示形状,B类表示颜色。(维度可以有多个,以组合方式实现)

在这里插入图片描述

二、示例

桥接(Bridge)模式包含以下主要角色:

  1. 抽象化(Abstraction)角色:定义抽象类,并包含一个对实现化对象的引用。
  2. 扩展抽象化(Refined Abstraction)角色:是抽象化角色的子类,实现父类中的业务方法,并通过组合关系调用实现化角色中的业务方法。
  3. 实现化(Implementor)角色:定义实现化角色的接口,供扩展抽象化角色调用。
  4. 具体实现化(Concrete Implementor)角色:给出实现化角色接口的具体实现。

在这里插入图片描述

1、Bridge.cpp

/*
 * C++ Design Patterns: Bridge
 * Author: Jakub Vojvoda [github.com/JakubVojvoda]
 * 2016
 *
 * Source code is licensed under MIT License
 * (for more details see LICENSE)
 *
 */

#include <iostream>

/*
 * Implementor
 * 实现化角色:定义实现化角色的接口,供扩展抽象化角色调用。
 */
class Implementor
{
public:
  virtual ~Implementor() {}
  
  virtual void action() = 0;
  // ...
};

/*
 * Concrete Implementors
 * 具体实现化角色:给出实现化角色接口的具体实现。
 */
class ConcreteImplementorA : public Implementor
{
public:
  ~ConcreteImplementorA() {}
  
  void action()
  {
    std::cout << "Concrete Implementor A" << std::endl;
  }
  // ...
};

class ConcreteImplementorB : public Implementor
{
public:
  ~ConcreteImplementorB() {}
  
  void action()
  {
    std::cout << "Concrete Implementor B" << std::endl;
  }
  // ...
};

/*
 * Abstraction
 * 抽象化角色:定义抽象类接口。
 */
class Abstraction
{
public:
  virtual ~Abstraction() {}
  
  virtual void operation() = 0;
  // ...
};

/*
 * RefinedAbstraction
 * 扩展抽象化角色:实现父类中的业务接口,并通过组合关系调用实现化角色中的业务方法。
 */
class RefinedAbstraction : public Abstraction
{
public:
  ~RefinedAbstraction() {}
  
  RefinedAbstraction(Implementor *impl) : implementor(impl) {}
  
  void operation()
  {
    implementor->action();
  }
  // ...

private:
  Implementor *implementor;
};


int main()
{
  Implementor *ia = new ConcreteImplementorA;
  Implementor *ib = new ConcreteImplementorB;
  
  Abstraction *abstract1 = new RefinedAbstraction(ia);
  abstract1->operation();
  
  Abstraction *abstract2 = new RefinedAbstraction(ib);
  abstract2->operation();
  
  delete abstract1;
  delete abstract2;
  
  delete ia;
  delete ib;
  
  return 0;
}

2、不同颜色的模型

在这里插入图片描述

在这里插入图片描述

#include <iostream>

class Color
{
public:
  virtual ~Color() {}
  
  virtual void show() = 0;
};

class Red : public Color
{
public:
  ~Red() {}
  
  void show() override
  {
    std::cout << "Color is red." << std::endl;
  }
};

class Blue : public Color
{
public:
  ~Blue() {}
  
  void show() override
  {
    std::cout << "Color is blue." << std::endl;
  }
};

class Model
{
public:
  virtual ~Model() {}
  
  virtual void show() = 0;
};

// 正方体模型
class CubeModel : public Model
{
public:
  ~CubeModel() {}
  
  CubeModel(Color *c) : color(c) {}
  
  void show() override
  {
    std::cout << "I am Cube." << std::endl;
    color->show();
  }

private:
  Color *color;
};

// 球体模型
class SphereModel : public Model
{
public:
  ~SphereModel() {}
  
  SphereModel(Color *c) : color(c) {}
  
  void show() override
  {
    std::cout << "I am Sphere." << std::endl;
    color->show();
  }

private:
  Color *color;
};


int main()
{
  Color *red = new Red();
  Color *blue = new Blue();
  
  CubeModel *cube = new CubeModel(red);
  cube->show();
  
  SphereModel *sphere = new SphereModel(blue);
  sphere->show();
  
  delete cube;
  delete sphere;
  
  delete red;
  delete blue;
  
  return 0;
}

三、优缺点,适用场景

优点

  • 符合开闭原则,抽象与实现分离,扩展能力强。
  • 单一职责原则,抽象部分专注于处理高层逻辑,实现部分处理平台细节。

缺点

  • 由于聚合关系建立在抽象层,要求开发者能正确地识别出系统中两个独立变化的维度。
  • 对高内聚的类使用该模式可能会让代码更加复杂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值