设计模式之桥接模式(C++)

简介: 设计模式之桥接模式(C++)

一、桥接模式是什么?

      桥接模式是一种结构型的软件设计模式,将抽象部分与实现部分分离,使他们可以独立地变化。


      举例来说,黑色钢笔、红色油笔、红色钢笔等等,如果颜色和笔类型合起来考虑,那类的复杂度将难以想象,若有10个颜色,10个笔类型,那要有10*10个类来涵盖所有类型的笔。但是如果拆分颜色和笔类型,通过组合的形式获得目标,那只要10+10个类即可。这样设计极大降低了系统复杂度和耦合程度。


      桥接模式的优点:


  1. 扩展性好。抽象与实现分离,扩展起来更便捷,可以获得更多样式的目标。
  2. 解耦。不同抽象间的耦合程度低。
  3. 满足设计模式要求的合成复用原则和开闭原则。
  4. 封装性好。具体实现细节对客户而言是透明不可见的。

     桥接模式的缺点:


  1. 使用场景有限制。只有系统有两个以上独立变化维度时才适用。

二、桥接模式

2.1 结构图

      客户端即Main主函数,不同的抽象组合完成想要的目标物,其中某个抽象要作为主体。

2.2 代码示例

      场景描述:我要通过软件画笔来绘制我要的图形,先选定笔型-圆珠笔,配置黑色,再选好图形-圆形,尺寸为20,完成绘制。

//Figure.h
/****************************************************/
#pragma once
#include <iostream>
using namespace std;
// 抽象类-图形
class Figure
{
public:
  // 创建
  virtual void create() = 0;
  // 获取尺寸
  int getSize() {
    return m_size;
  }
  // 设置尺寸
  void setSize(int size) {
    m_size = size;
  }
protected:
  int m_size = 10;
};
// 具体实现类-圆形
class Circle :public Figure
{
public:
  // 创建
  virtual void create() {
    cout << "○" << endl;
  }
};
// 具体实现类-矩形
class Rectangle :public Figure
{
public:
  // 创建
  virtual void create() {
    cout << "口" << endl;
  }
};
//Pen.h
/****************************************************/
#pragma once
#include <iostream>
#include <string>
#include "Figure.h"
using namespace std;
// 抽象类-笔
class Pen
{
public:
  // 构造函数
  Pen(Figure *figure){
    m_figure = figure;
  }
  // 析构函数
  virtual ~Pen() {
    if (m_figure != nullptr) {
      delete m_figure;
      m_figure = nullptr;
    }
  }
  // 绘制
  virtual void draw() = 0;
  // 获取颜色
  string getColor() {
    return m_color;
  }
  // 设置颜色
  void setColor(string color) {
    m_color = color;
  }
protected:
  string m_color = "白色";
  Figure *m_figure;
};
// 具体实现类-圆珠笔
class BallPen :public Pen
{
public:
  // 构造函数
  BallPen(Figure *figure) :Pen(figure) {};
  // 绘制
  virtual void draw() {
    cout << getColor() << "圆珠笔绘制尺寸为" << m_figure->getSize() << "的";
    m_figure->create();
  }
};
// 具体实现类-油画笔
class OilPen :public Pen
{
public:
  // 构造函数
  OilPen(Figure *figure) :Pen(figure) {};
  // 绘制
  virtual void draw() {
    cout << getColor() << "油画笔绘制尺寸为" << m_figure->getSize() << "的";
    m_figure->create();
  }
};
//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Figure.h"
#include "Pen.h"
using namespace std;
int main()
{
  Figure *figure1 = new Circle();
  figure1->setSize(20);
  Pen *pen1 = new BallPen(figure1);
  pen1->setColor("黑色");
  pen1->draw();
  figure1 = nullptr;
  delete pen1;
  pen1 = nullptr;
  return 0;
}

    程序结果如下。

      在上述示例中,因为Pen类中有一个m_figure,和figure1指向同一位置,所以在客户端中不能对figure1进行delete,不然会重复删除引发崩溃。在Qt中,许多类之间也有类似的设计。

三、总结

      我尽可能用较通俗的话语和直观的代码例程,来表述我对桥接模式的理解,或许有考虑不周到的地方,如果你有不同看法欢迎评论区交流!希望我举的例子能帮助你更好地理解桥接模式。

      如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

相关文章
|
1月前
|
设计模式 安全 测试技术
【C/C++ 设计模式 单例】单例模式的选择策略:何时使用,何时避免
【C/C++ 设计模式 单例】单例模式的选择策略:何时使用,何时避免
61 0
|
1月前
|
设计模式 算法 C++
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程(二)
【C++ 泛型编程 进阶篇】C++元模板编程与设计模式的结合应用教程
26 0
|
1月前
|
设计模式 存储 uml
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
29 1
|
1月前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
1月前
|
设计模式
设计模式之桥接模式
设计模式之桥接模式
|
1月前
|
设计模式 Linux Windows
【设计模式】桥接模式
【设计模式】桥接模式
|
2月前
|
设计模式 Oracle Java
设计模式--- 桥接模式、JDBC 源码剖析(桥接)
设计模式--- 桥接模式、JDBC 源码剖析(桥接)
49 2
|
3天前
|
设计模式 存储 Java
C++从入门到精通:3.5设计模式——提升代码可维护性与可扩展性的关键
C++从入门到精通:3.5设计模式——提升代码可维护性与可扩展性的关键
|
5天前
|
设计模式
【设计模式系列笔记】桥接模式
桥接模式(Bridge Pattern)是一种结构性设计模式,它将抽象部分与实现部分分离,使它们可以独立变化而互不影响。桥接模式通过组合而不是继承的方式来实现这种分离。
27 6
|
30天前
|
设计模式 算法 中间件
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
【C++ 可调用对象的应用】C++设计模式与现代编程技巧:深入可调用对象的世界
114 1