文章目录
- [1. 定义](#1. 定义)
- [2. 结构组成](#2. 结构组成)
- [3. 桥接模式结构](#3. 桥接模式结构)
- [4. 示例代码](#4. 示例代码)
- [5. 模式优势](#5. 模式优势)
- [6. 总结](#6. 总结)
1. 定义
桥接模式(Bridge Pattern)属于结构型设计模式,它的主要目的是将抽象部分与它的实现部分分离,使它们都可以独立地变化。
2. 结构组成
桥接模式通常包含以下几个关键角色:
抽象化(Abstraction):
- 这是桥接模式中的高层抽象类,它定义了抽象接口,通常包含了对行为的抽象方法。
- 它持有一个对实现化对象的引用,通过这个引用可以调用具体的实现方法。
- 抽象类可以有多个具体的抽象子类(Refined Abstraction),这些子类继承自抽象类,并可以添加自己的方法。
实现化(Implementation):
- 这是一个接口或抽象类,定义了具体实现的方法。
- 这个接口的具体实现类(Concrete Implementations)会提供这些方法的具体实现。
具体实现类(Concrete Implementations):
- 这些是实现化接口(Implementation)的具体类。
- 每个具体实现类都提供了接口中定义的方法的具体实现。
细化抽象类(Refined Abstraction):
- 这些是抽象类(Abstraction)的具体子类。
- 它们继承了抽象类的接口,并可以添加自己的方法。
- 通过调用抽象类中的实现类引用,可以调用具体实现类中的方法。
客户端(Client):
- 客户端使用抽象类(Abstraction)的接口。
- 客户端调用抽象类的方法,而具体的实现由具体实现类(Concrete Implementations)来完成。
3. 桥接模式结构

- 
客户端(Client):使用抽象类(Abstraction)的接口。 
- 
抽象化(Abstraction):定义抽象接口,并持有实现化对象的引用。 
- 
实现化(Implementation):定义具体实现的方法。 
- 
具体实现类(Concrete Implementations):提供实现化接口中方法的具体实现。 
- 
细化抽象类(Refined Abstraction):抽象类的具体子类,继承抽象接口并可添加自己的方法。 
4. 示例代码
            
            
              cpp
              
              
            
          
          #include <iostream>
#include <memory>
using namespace std;
class Implementation{
public:
    Implementation(){cout << "Implementation construction" <<endl;}
    virtual ~Implementation(){ cout<< "Implementation destruction" <<endl; }
    virtual string OperationImplementation() const = 0;
};
class ConcreteImplementationA : public Implementation{
public:
    ConcreteImplementationA(){cout << "ConcreteImplementationA construction" <<endl;}
    ~ConcreteImplementationA(){cout << "ConcreteImplementationA destruction" <<endl;}
    string OperationImplementation() const override { 
        return "ConcreteImplementationA: Here's the result on the platform A.\n"; 
    }
};
class ConcreteImplementationB : public Implementation{
public:
    ConcreteImplementationB(){cout << "ConcreteImplementationB construction" <<endl;}
    ~ConcreteImplementationB(){cout << "ConcreteImplementationB destruction" <<endl;}
    
    string OperationImplementation() const override { 
        return "ConcreteImplementationB: Here's the result on the platform B.\n"; 
    }
};
class Abstraction{
protected:
    shared_ptr<Implementation> implementation_;
public:
    Abstraction(shared_ptr<Implementation> implementation) : implementation_(implementation){
        cout << "Abstraction construction" <<endl;
    }
    
    virtual ~Abstraction() {
        cout << "Abstraction destruction" <<endl;
    }
    
    virtual string Operation() const{
        return "Abstraction: Base operation with:\n" + 
            implementation_->OperationImplementation();    
    } 
};
class ExtendedAbstraction : public Abstraction{
public:
    ExtendedAbstraction(shared_ptr<Implementation> implementation) : Abstraction(implementation){
        cout << "ExtendedAbstraction construction" <<endl;
    }
    
    ~ExtendedAbstraction(){cout << "ExtendedAbstraction destruction" <<endl;}
    
    string Operation() const override{
        return "ExtendedAbstraction: Base operation with:\n" + 
            implementation_->OperationImplementation();    
    } 
};
void client( shared_ptr<Abstraction> abstract){
    cout<< abstract->Operation() <<endl;    
}
int main()
{
    shared_ptr<Implementation>  implementationA =  make_shared<ConcreteImplementationA>();
    shared_ptr<Abstraction>  abstraction =  make_shared<Abstraction>(implementationA);
    client(abstraction);
    
    shared_ptr<Implementation>  implementationB =  make_shared<ConcreteImplementationB>();
    shared_ptr<Abstraction>  exabstraction =  make_shared<ExtendedAbstraction>(implementationB);
    client(exabstraction);
        
    return 0;
}5. 模式优势
- 
分离抽象和实现:使得抽象和实现可以独立地变化,互不影响,提高了系统的可扩展性和可维护性。 
- 
提高灵活性:通过组合的方式使用实现类,使得在运行时可以灵活地选择和切换实现类。 
- 
符合开闭原则:可以在不修改现有代码的情况下增加新的抽象类或具体实现类,符合开闭原则(对扩展开放,对修改关闭)。 
- 
减少子类数量:避免了在抽象和实现之间使用多层继承导致的子类数量爆炸问题。 
6. 总结
桥接模式通过将抽象和实现分离,提供了一种优雅的方式来处理具有多维度变化的系统。它允许开发者在不影响现有代码的情况下,灵活地扩展抽象部分和实现部分。这种模式在处理如跨平台应用开发、不同数据库访问操作等场景时非常有用。通过使用组合而非继承,桥接模式提高了代码的复用性和可维护性,同时也使得系统架构更加清晰和易于理解。