ITPub博客

首页 > 应用开发 > IT综合 > 适配器模式

适配器模式

原创 IT综合 作者:dongyu2013 时间:2014-03-25 08:25:08 0 删除 编辑
系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。
适配器主要分为类适配器和对象适配器
这里主要讲对象适配器

点击(此处)折叠或打开

  1. class Target{
  2. public:
  3.    virtual void Request()
  4.    {
  5.        cout<<\"普通请求\"<<endl;
  6.    }
  7. };

  8. class Adaptee //需要适配的类
  9. {
  10. public:
  11.     void SpecificRequest()
  12.     {
  13.      cout<<\"特殊请求\"<<endl;
  14.     }
  15. };

  16. class Adapter:public Target
  17. {
  18. private:
  19.     Adaptee adaptee;
  20. public:
  21.     void Request()
  22.     {
  23.      adaptee.SpecificRequest();
  24.     }
  25. };

  26. int main(void)
  27. {
  28.     Target target=new Adapter();
  29.     target.Request();
  30.     return 0;
  31. }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1128805/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-12-25

  • 博文量
    263
  • 访问量
    238065