桥接模式
桥接模式可将一个大类或一系列紧密相关的类拆分为抽象和实现两个独立的层次结构, 从而能在开发时分别使用。解决了在独立的维度上扩展类遇到的组合种类爆炸的问题。
说明:文章插图与代码均引用自 refactoringguru.com 网站,但代码解释为原创中文翻译,转载请注明来源。
UML 示意图
C++ 代码实现
基础概念的实现
/**
* The Implementation defines the interface for all implementation classes. It
* doesn't have to match the Abstraction's interface. In fact, the two
* interfaces can be entirely different. Typically the Implementation interface
* provides only primitive operations, while the Abstraction defines higher-
* level operations based on those primitives.
* 桥接模式主要的思想是用“抽象“与“实现” 来完成对类的扩展
* Implementation 定义了所有的“实现“ 类的接口,不一定要和 Abstraction 的接口一模一样,
* 可以完全不同。通常 Implementation 的接口提供原语(primitive)操作,而 Abstraction 基于这些原
* 语,定义了更高级的操作
*/
class Implementation {
public:
virtual ~Implementation() {}
virtual std::string OperationImplementation() const = 0;
};
/**
* Each Concrete Implementation corresponds to a specific platform and
* implements the Implementation interface using that platform's API.
* 每个具体的实现和一个特定的"平台"有关,并且使用这个"平台"的 API 实现 Implementation 的接口
*/
class ConcreteImplementationA : public Implementation {
public:
std::string OperationImplementation() const override {
return "ConcreteImplementationA: Here's the result on the platform A.\n";
}
};
class ConcreteImplementationB : public Implementation {
public:
std::string OperationImplementation() const override {
return "ConcreteImplementationB: Here's the result on the platform B.\n";
}
};
/**
* The Abstraction defines the interface for the "control" part of the two class
* hierarchies. It maintains a reference to an object of the Implementation
* hierarchy and delegates all of the real work to this object.
* Abstraction 定义了两个类控制部分的层次结构的接口。它维持一个 Implementation 层次结构对象的
* 一个引用,并且将实际工作派活给这个对象
*/
class Abstraction {
/**
* @var Implementation
*/
protected:
// Implementation 的指针
Implementation* implementation_;
public:
Abstraction(Implementation* implementation) : implementation_(implementation) {
}
virtual ~Abstraction() {
}
virtual std::string Operation() const {
return "Abstraction: Base operation with:\n" +
this->implementation_->OperationImplementation();
}
};
/**
* You can extend the Abstraction without changing the Implementation classes.
* 不用更改 Implementation 类就可以扩展 Abstraction
*/
class ExtendedAbstraction : public Abstraction {
public:
ExtendedAbstraction(Implementation* implementation) : Abstraction(implementation) {
}
std::string Operation() const override {
return "ExtendedAbstraction: Extended operation with:\n" +
this->implementation_->OperationImplementation();
}
};
/**
* Except for the initialization phase, where an Abstraction object gets linked
* with a specific Implementation object, the client code should only depend on
* the Abstraction class. This way the client code can support any abstraction-
* implementation combination.
* 除了初始化阶段Abstraction类对象拿到了一个具体Implementation类对象外,
* 客户端代码应该只依赖抽象类,用这种方式,客户端代码能支持任意抽象-实现的组合。
*/
void ClientCode(const Abstraction& abstraction) {
// ...
std::cout << abstraction.Operation();
// ...
}
/**
* The client code should be able to work with any pre-configured abstraction-
* implementation combination.
* 客户端代码应该能在任意预先设置好的 abstraction-implementation 组合工作
*/
int main() {
Implementation* implementation = new ConcreteImplementationA;
// 抽象类的实例需要一个实现类的实例
Abstraction* abstraction = new Abstraction(implementation);
ClientCode(*abstraction);
std::cout << std::endl;
delete implementation;
delete abstraction;
implementation = new ConcreteImplementationB;
abstraction = new ExtendedAbstraction(implementation);
ClientCode(*abstraction);
delete implementation;
delete abstraction;
return 0;
}
运行结果
Abstraction: Base operation with:
ConcreteImplementationA: Here's the result on the platform A.
ExtendedAbstraction: Extended operation with:
ConcreteImplementationB: Here's the result on the platform B.