设计模式[13]-Bridge

简介:

Type: Structural

Bridge: 将抽象与其实现解耦,这样两者可以独立地变化。

#include <iostream>
using namespace std;

class Implementor
{
public:
    virtual void operationImpl() = 0;
};

class ConcreteImplementorA: public Implementor
{
public:
    void operationImpl()
    {
        cout<<"ConcreteImplementorA operationImpl"<<endl;
    };
};

class ConcreteImplementorB: public Implementor
{
public:
    void operationImpl()
    {
        cout<<"ConcreteImplementorB operationImpl"<<endl;
    };
};

class Abstraction
{
public:
    void setImplementor(Implementor* pImplementor)
    {
        m_pImplementor = pImplementor;
    };
    void operation()
    {
        if(m_pImplementor != NULL)
            m_pImplementor->operationImpl();
    };
private:
    Implementor* m_pImplementor;
};

int main()
{
    ConcreteImplementorA *pImplA = new ConcreteImplementorA();
    ConcreteImplementorB *pImplB = new ConcreteImplementorB();
    Abstraction *pAbstraction = new Abstraction;
    pAbstraction->setImplementor(pImplA);
    pAbstraction->operation();
    pAbstraction->setImplementor(pImplB);
    pAbstraction->operation();

    system("pause");
    return 0;
}

目录
相关文章
|
5月前
|
设计模式 Java 关系型数据库
认真学习设计模式之桥接模式(Bridge Pattern)
认真学习设计模式之桥接模式(Bridge Pattern)
36 0
|
6月前
|
设计模式 关系型数据库
设计模式11 - 桥梁模式【Bridge Pattern】
设计模式11 - 桥梁模式【Bridge Pattern】
24 0
|
6月前
|
设计模式 Java Unix
【设计模式——学习笔记】23种设计模式——桥接模式Bridge(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——桥接模式Bridge(原理讲解+应用场景介绍+案例介绍+Java代码实现)
54 0
|
6月前
|
设计模式 开发者
设计模式~桥接模式(bridge)-14
(1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例: 代码 桥接(Bridge)是
31 0
|
10月前
|
设计模式
聊聊设计模式中的Bridge模式
聊聊设计模式中的Bridge模式
62 0
|
11月前
|
设计模式 Java
Java设计模式-桥接模式(Bridge Pattern)
Java设计模式-桥接模式(Bridge Pattern)
|
设计模式 Unix API
设计模式学习(一):Bridge桥接模式
Bridge模式的作用是在“类的功能层次结构”和“类的实现层次结构”之间搭建桥梁。
104 0
设计模式学习(一):Bridge桥接模式
|
设计模式 开发者
从零开始学设计模式(十): 桥接模式(Bridge Pattern)
桥接(Bridge)模式又称为柄体(Handle and Body)模式或接口(Interface)模式,它将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。
157 0
从零开始学设计模式(十): 桥接模式(Bridge Pattern)
|
设计模式 Java
【Java设计模式】喝一杯Java茶吧,带你掌握桥接模式(Bridge)
【Java设计模式】喝一杯Java茶吧,带你掌握桥接模式(Bridge)
【Java设计模式】喝一杯Java茶吧,带你掌握桥接模式(Bridge)
|
设计模式 开发者
【愚公系列】2021年12月 二十三种设计模式(七)-桥接模式(Bridge Pattern)
【愚公系列】2021年12月 二十三种设计模式(七)-桥接模式(Bridge Pattern)
【愚公系列】2021年12月 二十三种设计模式(七)-桥接模式(Bridge Pattern)