/** * @name 适配器模式 * @brief: 转换接口以兼容客户端 * @note 场景:集成旧库、第三方库接口转换 */ // 旧类 (不兼容接口) class LegacyPrinter { public: void printDocument() { /* 旧式打印 */ } }; class PrinterInterface { public: virtual void print() = 0; }; // 适配器 class PrinterAdapter : public PrinterInterface { LegacyPrinter legacyPrinter; public: void print() override { legacyPrinter.printDocument(); // 调用旧方法 } }; // 使用 int main() { PrinterInterface* printer = new PrinterAdapter(); printer->print(); }