C++中的适配器模式

适配器模式(Adapter Pattern)

适配器模式是一种结构型设计模式,它使得原本由于接口不兼容而不能一起工作的类可以协同工作。适配器模式通过将一个类的接口转换成客户端希望的另一种接口,使得原本接口不兼容的类可以一起工作。适配器可以是对象适配器或类适配器,对象适配器使用组合,类适配器使用多重继承。

实际应用

图形渲染库适配器

假设我们有一个旧的图形渲染库和一个新的图形渲染接口,我们需要使旧的库适配新的接口。

 
  1. #include <iostream>

  2. // 旧的图形渲染库

  3. class OldGraphicsRenderer {

  4. public:

  5. void drawCircle(float x, float y, float radius) {

  6. std::cout << "Old Renderer: Drawing Circle at (" << x << ", " << y << ") with radius " << radius << "\n";

  7. }

  8. void drawRectangle(float x, float y, float width, float height) {

  9. std::cout << "Old Renderer: Drawing Rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << "\n";

  10. }

  11. };

  12. // 新的图形渲染接口

  13. class NewGraphicsRenderer {

  14. public:

  15. virtual void renderCircle(float x, float y, float radius) = 0;

  16. virtual void renderRectangle(float x, float y, float width, float height) = 0;

  17. };

  18. // 适配器类,将旧的渲染库适配到新的接口

  19. class GraphicsRendererAdapter : public NewGraphicsRenderer {

  20. private:

  21. OldGraphicsRenderer* oldRenderer;

  22. public:

  23. GraphicsRendererAdapter(OldGraphicsRenderer* renderer) : oldRenderer(renderer) {}

  24. void renderCircle(float x, float y, float radius) override {

  25. oldRenderer->drawCircle(x, y, radius);

  26. }

  27. void renderRectangle(float x, float y, float width, float height) override {

  28. oldRenderer->drawRectangle(x, y, width, height);

  29. }

  30. };

  31. int main() {

  32. OldGraphicsRenderer oldRenderer;

  33. GraphicsRendererAdapter adapter(&oldRenderer);

  34. adapter.renderCircle(10, 10, 5);

  35. adapter.renderRectangle(20, 20, 10, 5);

  36. return 0;

  37. }

日志系统适配器

假设我们有一个旧的日志系统和一个新的日志系统接口,我们需要使旧的日志系统适配新的接口。

 
  1. #include <iostream>

  2. #include <string>

  3. // 旧的日志系统

  4. class OldLogger {

  5. public:

  6. void logMessage(const std::string& msg) {

  7. std::cout << "Old Logger: " << msg << "\n";

  8. }

  9. };

  10. // 新的日志系统接口

  11. class NewLogger {

  12. public:

  13. virtual void info(const std::string& msg) = 0;

  14. virtual void error(const std::string& msg) = 0;

  15. };

  16. // 适配器类,将旧的日志系统适配到新的接口

  17. class LoggerAdapter : public NewLogger {

  18. private:

  19. OldLogger* oldLogger;

  20. public:

  21. LoggerAdapter(OldLogger* logger) : oldLogger(logger) {}

  22. void info(const std::string& msg) override {

  23. oldLogger->logMessage("INFO: " + msg);

  24. }

  25. void error(const std::string& msg) override {

  26. oldLogger->logMessage("ERROR: " + msg);

  27. }

  28. };

  29. int main() {

  30. OldLogger oldLogger;

  31. LoggerAdapter adapter(&oldLogger);

  32. adapter.info("This is an info message");

  33. adapter.error("This is an error message");

  34. return 0;

  35. }

支付系统适配器

假设我们有一个旧的支付系统和一个新的支付接口,我们需要使旧的支付系统适配新的接口。

 
  1. #include <iostream>

  2. #include <string>

  3. // 旧的支付系统

  4. class OldPaymentSystem {

  5. public:

  6. void makePayment(double amount, const std::string& currency) {

  7. std::cout << "Old Payment System: Processing payment of " << amount << " " << currency << "\n";

  8. }

  9. };

  10. // 新的支付接口

  11. class NewPaymentInterface {

  12. public:

  13. virtual void pay(double amount) = 0;

  14. };

  15. // 适配器类,将旧的支付系统适配到新的接口

  16. class PaymentAdapter : public NewPaymentInterface {

  17. private:

  18. OldPaymentSystem* oldPaymentSystem;

  19. public:

  20. PaymentAdapter(OldPaymentSystem* paymentSystem) : oldPaymentSystem(paymentSystem) {}

  21. void pay(double amount) override {

  22. oldPaymentSystem->makePayment(amount, "USD");

  23. }

  24. };

  25. int main() {

  26. OldPaymentSystem oldPaymentSystem;

  27. PaymentAdapter adapter(&oldPaymentSystem);

  28. adapter.pay(100.0);

  29. return 0;

  30. }

总结

适配器类通过包含或继承旧系统类,并实现新接口的方法,从而将旧系统的方法适配到新接口上。

  • 32
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配器模式(Adapter Pattern)是一种结构型设计模式,它可以将一个类的接口转换成客户端所期望的另一种接口。这种模式常用来解决两个已有的接口之间不兼容的问题。 下面是一个简单的 C++ 适配器模式的例子: ```cpp #include <iostream> using namespace std; // 新的接口 class Target { public: virtual void request() = 0; }; // 老的接口 class Adaptee { public: void specificRequest() { cout << "Adaptee::specificRequest()" << endl; } }; // 适配器 class Adapter : public Target { public: Adapter(Adaptee* adaptee) { m_adaptee = adaptee; } void request() { m_adaptee->specificRequest(); } private: Adaptee* m_adaptee; }; int main() { // 创建老的接口对象 Adaptee* adaptee = new Adaptee(); // 创建适配器 Target* target = new Adapter(adaptee); // 通过新的接口调用老的接口 target->request(); return 0; } ``` 在上面的示例,我们有一个新的接口 `Target` 和一个老的接口 `Adaptee`,它们之间的接口不兼容。我们使用适配器 `Adapter` 将 `Adaptee` 的接口转换成 `Target` 的接口。 在 `Adapter` ,我们使用 `Adaptee` 的实例来实现 `Target` 的接口。这样就可以通过 `Target` 接口来访问 `Adaptee` 的功能了。 在 `main` 函数,我们首先创建了 `Adaptee` 的实例,然后创建了一个适配器 `Adapter`,并将 `Adaptee` 的实例传递给它。最后,我们通过新的接口 `Target` 来调用老的接口 `Adaptee`。 适配器模式可以很好地解决两个不兼容接口之间的问题,但是需要注意适配器的设计和实现,以确保适配器的正确性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值