本文主要是介绍设计模式之桥接模式 - 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!