结构型模式之桥接模式

ops/2024/10/10 15:53:51/

文章目录

  • 概述
  • 原理
  • 结构图
    • 代码示例
  • 小结

概述

桥接模式(bridge pattern) 的定义是:将抽象部分与它的实现部分分离,使它们都可以独立地变化。

桥接模式用一种巧妙的方式处理多层继承存在的问题,用抽象关联来取代传统的多层继承,将类之间的静态继承关系转变为动态的组合关系,使得系统更加灵活,并易于扩展,有效的控制了系统中类的个数 (避免了继承层次的指数级爆炸).

原理

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

  • 抽象化(Abstraction)角色 :主要负责定义出该角色的行为 ,并包含一个对实现化对象的引用。
  • 扩展抽象化(RefinedAbstraction)角色 :是抽象化角色的子类,实现父类中的业务方法,并通过组合关系调用实现化角色中的业务方法。
  • 实现化(Implementor)角色 :定义实现化角色的接口,包含角色必须的行为和属性,并供扩展抽象化角色调用。
  • 具体实现化(Concrete Implementor)角色 :给出实现化角色接口的具体实现。

结构图

在这里插入图片描述

代码示例

来看下代码示例吧,如下图:

// Implementor.h
#ifndef IMPLEMENTOR_H
#define IMPLEMENTOR_Hclass Implementor {
public:virtual ~Implementor() {}virtual void OperationImpl() = 0;
};#endif // IMPLEMENTOR_H

// ConcreteImplementorA.h
#ifndef CONCRETEIMPLEMENTORA_H
#define CONCRETEIMPLEMENTORA_H#include "Implementor.h"class ConcreteImplementorA : public Implementor {
public:void OperationImpl() override {// Concrete implementation Astd::cout << "Concrete Implementor A" << std::endl;}
};#endif // CONCRETEIMPLEMENTORA_H
// ConcreteImplementorB.h
#ifndef CONCRETEIMPLEMENTORB_H
#define CONCRETEIMPLEMENTORB_H#include "Implementor.h"class ConcreteImplementorB : public Implementor {
public:void OperationImpl() override {// Concrete implementation Bstd::cout << "Concrete Implementor B" << std::endl;}
};
// Abstraction.h
#ifndef ABSTRACTION_H
#define ABSTRACTION_H#include "Implementor.h"class Abstraction {
protected:Implementor* implementor;public:Abstraction(Implementor* implementor) : implementor(implementor) {}virtual ~Abstraction() { delete implementor; }virtual void Operation() = 0;
};
/ RefinedAbstraction.h
#ifndef REFINEDABSTRACTION_H
#define REFINEDABSTRACTION_H#include "Abstraction.h"class RefinedAbstraction : public Abstraction {
public:RefinedAbstraction(Implementor* implementor) : Abstraction(implementor) {}void Operation() override {// Refined operationstd::cout << "Refined Abstraction" << std::endl;implementor->OperationImpl();}
};
/ main.cpp
#include <iostream>
#include "Abstraction.h"
#include "ConcreteImplementorA.h"
#include "ConcreteImplementorB.h"
#include "RefinedAbstraction.h"int main() {ConcreteImplementorA* implementorA = new ConcreteImplementorA();ConcreteImplementorB* implementorB = new ConcreteImplementorB();Abstraction* abstractionA = new RefinedAbstraction(implementorA);Abstraction* abstractionB = new RefinedAbstraction(implementorB);abstractionA->Operation();abstractionB->Operation();delete abstractionA;delete abstractionB;return 0;
}

小结

上边有桥接模式的概述,原理,以及代码示例。看起来不错吧,感兴趣,可以一起学习学习。


http://www.ppmy.cn/ops/43931.html

相关文章

pinia的使用

搭建pinia环境 pinia&#xff1a;集中式状态管理工具&#xff0c;用于各组件之间共享数据&#xff08;多个组件会用到的数据才考虑放到pinia中&#xff09; 在vue2中使用的是vuex 1.终端输入&#xff1a;npm i pinia 2. 在vue组件中出现pinia 用pinia存储读取数据 要想好…

Yolov5保姆及入门-含源码【推荐】

前言 YOLO系列模型作为一种实时目标检测算法&#xff0c;自从YOLO1发布以来&#xff0c;就以其检测速度快、准确率高而受到广泛关注。随着技术的迭代&#xff0c;YOLO系列已经发展到了YOLO8。本文将详细介绍YOLO5的技术规格、应用场景、特点以及性能对比。 yolov5源码下载地址…

Python图形界面(GUI)Tkinter笔记(八):用【Label()】方法制作九九乘数表

主要是使用"config()"方法来体现函数式、模块化的美好风景。把需随时要修改的控件参数定义在“config()”方法里且把它封装在一个函数中&#xff0c;这时只需对这函数内的“config()”方法作出相应的修改即可&#xff0c;无需对主代码或全部代码重新修一遍。这也是Py…

python-docx 在word中指定位置插入图片或表格

docx库add_picture()方法不支持对图片位置的设置 1、新建一个1行3列的表格&#xff0c;在中间的一列中插入图片 from docx import Document from docx.shared import Pt from docx.oxml.shared import OxmlElement from docx.enum.text import WD_ALIGN_PARAGRAPHdef add_cen…

LeetCode 题解:112. 路径总和,递归,JavaScript,详细注释

原题链接&#xff1a; 112. 路径总和 解题思路&#xff1a; 如果求根节点到叶子节点的路径上的节点值之和&#xff0c;假设共有3个节点&#xff0c;那么写成计算式是val1 val2 val3 sum那么将计算式转换就可以得到val3 sum - val1 - val2也就是说&#xff0c;问题可以从…

PyTorch Conv2d 前向传递中发生了什么?

在PyTorch中,nn.Conv2d 是一个用于二维卷积操作的模块。当你对一个输入张量(通常是一个四维的,形状为 [batch_size, channels, height, width])调用 forward 方法(或直接使用模块作为函数调用)时,Conv2d 会执行一系列的计算来产生输出张量。 以下是 Conv2d 前向传递中发…

NIO的ByteBuffer和Netty的ByteBuf的性能

在讨论Java NIO的ByteBuffer与Netty的ByteBuf的性能时&#xff0c;需要考虑几个主要的因素&#xff0c;因为性能表现并不是绝对的&#xff0c;而是依赖于具体的使用场景。Netty的ByteBuf设计更加现代&#xff0c;针对网络编程的需求进行了优化&#xff0c;包含了许多ByteBuffer…

C++ RPC ORM 高速解析

支持所有常用编程语 https://capnproto.org/GitHub - capnproto/capnproto: Capn Proto serialization/RPC system - core tools and C library https://capnproto.org/capnproto-c-win32-1.0.2.zip 常用命令&#xff1a; capnp help capnp compile -oc myschema.capn…