设计模式之桥接模式 - 202201

2024-05-11 17:32

本文主要是介绍设计模式之桥接模式 - 202201,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

模式动机

设想如果要绘制矩形、圆形、椭圆、正方形,我们至少需要4个形状类,但是如果绘制的图形需要具有不同的颜色,如红色、绿色、蓝色等,此时至少有如下两种设计方案:

  • 第一种设计方案是为每一种形状都提供一套各种颜色的版本。
  • 第二种设计方案是根据实际需要对形状和颜色进行组合

对于有两个变化维度(即两个变化的原因)的系统,采用方案二来进行设计系统中类的个数更少,且系统扩展更为方便。设计方案二即是桥接模式的应用。桥接模式将继承关系转换为关联关系,从而降低了类与类之间的耦合,减少了代码编写量。

模式定义

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

模式结构

桥接模式包含如下角色:

  • Abstraction:抽象类
  • RefinedAbstraction:扩充抽象类
  • Implementor:实现类接口
  • ConcreteImplementor:具体实现类

时序图

 代码-裸指针版本

// BradgePattern.h
//
// Created by zhaoyf on 2022/4/16.
//#ifndef CPPDESIGNPATTERN_BRIDGEPATTERN_H
#define CPPDESIGNPATTERN_BRIDGEPATTERN_Hnamespace BridgePattern {// Implementorclass Implementor {public:virtual ~Implementor() {}virtual void Action() = 0;};// ConcreteImplementorAclass ConcreteImplementorA : public Implementor {public:virtual ~ConcreteImplementorA() {}virtual void Action();};// ConcreteImplementorBclass ConcreteImplementorB : public Implementor {public:virtual ~ConcreteImplementorB() {}virtual void Action();};// Abstraction, 抽象class Abstraction {public:virtual ~Abstraction() {}virtual void operation() = 0;};// RefinedAbstractionclass RefinedAbstraction : public Abstraction {public:RefinedAbstraction(Implementor *p);virtual ~RefinedAbstraction();virtual void operation() override;private:Implementor *pImpl;};
}#endif //CPPDESIGNPATTERN_BRIDGEPATTERN_H
// BradgePattern.cpp
//
// Created by zhaoyf on 2022/4/16.
//#include "BridgePattern.h"
#include "iostream"namespace BridgePattern {// ConcreteImplementorAvoid ConcreteImplementorA::Action() {std::cout << "ConcreteImplementorA::Action" << std::endl;}// ConcreteImplementorBvoid ConcreteImplementorB::Action() {std::cout << "ConcreteImplementorB::Action" << std::endl;}// RefinedAbstractionRefinedAbstraction::RefinedAbstraction(Implementor *p) : pImpl(p) {}RefinedAbstraction::~RefinedAbstraction() {if (pImpl) {delete pImpl;pImpl = nullptr;}}void RefinedAbstraction::operation() {pImpl->Action();}
}
// main.cpp 测试代码int main() {Implementor *pImplA = new ConcreteImplementorA();Abstraction *pa = new RefinedAbstraction(pImplA);pa->operation();Implementor *pImplB = new ConcreteImplementorB();Abstraction *pb = new RefinedAbstraction(pImplB);pb->operation();delete pa;delete pb;return 0;}output:
ConcreteImplementorA::Action
ConcreteImplementorB::Action

代码-智能指针版本

// BridgePatternSharedPtr.h
//
// Created by zhaoyf on 2022/4/16.
//#ifndef CPPDESIGNPATTERN_BRIDGEPATTERN_SHAREDPTR_H
#define CPPDESIGNPATTERN_BRIDGEPATTERN_SHAREDPTR_H#include <memory>namespace BridgePatternSharedPtr {// Implementorclass Implementor {public:virtual ~Implementor() {}virtual void Action() = 0;};// ConcreteImplementorAclass ConcreteImplementorA : public Implementor {public:virtual ~ConcreteImplementorA() {}virtual void Action();};// ConcreteImplementorBclass ConcreteImplementorB : public Implementor {public:virtual ~ConcreteImplementorB() {}virtual void Action();};// Abstraction, 抽象class Abstraction {public:virtual ~Abstraction() {}virtual void operation() = 0;};// RefinedAbstractionclass RefinedAbstraction : public Abstraction {public:RefinedAbstraction(std::unique_ptr<Implementor> p);virtual ~RefinedAbstraction() {}virtual void operation() override;private:std::unique_ptr<Implementor> pImpl;};}#endif //CPPDESIGNPATTERN_BRIDGEPATTERN_SHAREDPTR_H
// BridgePatternSharedPtr.cpp
//
// Created by zhaoyf on 2022/4/16.
//#include "BridgePattern_SharedPtr.h"
#include "iostream"namespace BridgePatternSharedPtr {// ConcreteImplementorAvoid ConcreteImplementorA::Action() {std::cout << "ConcreteImplementorA::Action" << std::endl;}// ConcreteImplementorBvoid ConcreteImplementorB::Action() {std::cout << "ConcreteImplementorB::Action" << std::endl;}// RefinedAbstractionRefinedAbstraction::RefinedAbstraction(std::unique_ptr<Implementor> p) : pImpl(std::move(p)) {}void RefinedAbstraction::operation() {pImpl->Action();}
}
// main.cpp 测试代码int main() {auto pImplA = std::make_unique<ConcreteImplementorA>();auto pa = std::make_unique<RefinedAbstraction>(std::move(pImplA));pa->operation();auto pb = std::make_unique<RefinedAbstraction>(std::make_unique<ConcreteImplementorB>());pb->operation();return 0;}output:
ConcreteImplementorA::Action
ConcreteImplementorB::Action

站在别人的肩旁上,感谢,参考文章如下:

2. 桥接模式 — Graphic Design Patterns

https://github.com/JakubVojvoda/design-patterns-cpp/tree/master/bridge

个人代码仓库

Cpp_Design_Pattern: 用C++实现的设计模式学习

这篇关于设计模式之桥接模式 - 202201的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980190

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素

《x86汇编语言:从实模式到保护模式》视频来了

《x86汇编语言:从实模式到保护模式》视频来了 很多朋友留言,说我的专栏《x86汇编语言:从实模式到保护模式》写得很详细,还有的朋友希望我能写得更细,最好是覆盖全书的所有章节。 毕竟我不是作者,只有作者的解读才是最权威的。 当初我学习这本书的时候,只能靠自己摸索,网上搜不到什么好资源。 如果你正在学这本书或者汇编语言,那你有福气了。 本书作者李忠老师,以此书为蓝本,录制了全套视频。 试