Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2565206
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: 项目管理

2012-09-30 14:29:46

adapter.h

点击(此处)折叠或打开

  1. /*
  2.  * adapter.h
  3.  *
  4.  * Created on: Sep 20, 2012
  5.  * Author: hl
  6.  * 感觉改了这么多,最终就是封装了一个包含所有的方法的common类,类中实现了所有情况的方法,然后根据情况去调用相应
  7.  * 的方法来实现适配!好麻烦啊,不过这样可以不用去动本公司的代码和其他公司的代码!
  8.  */

  9. #ifndef _ADAPTER_H_
  10. #define _ADAPTER_H_
  11. #include <iostream>
  12. #include <string>
  13. #include <map>

  14. using namespace std;

  15. class CommonUser
  16. {
  17. public:
  18.     virtual string getName() = 0;
  19.     virtual string getMobile() = 0;
  20. };

  21. //----------------该部分可以不用动!就这点好处---------------------------
  22. /**
  23.  * @brief 本公司员工管理
  24.  */
  25. class UserInfo
  26. {
  27. public:
  28.     string getName();
  29.     string getMobile();
  30. };

  31. /**
  32.  * @brief 其他公司员工管理
  33.  */
  34. class OtherInfo
  35. {
  36. public:
  37.     map<int, string> * getBasicInfo();
  38. };
  39. //------------------------------------------

  40. /**
  41.  * brief 为了实现适配器
  42.  */
  43. class Function
  44. {
  45. public:
  46.     Function()
  47.     {
  48.         pmp = oinf.getBasicInfo();
  49.     }
  50.     ~Function()
  51.     {
  52.         delete pmp;
  53.     }
  54. public:
  55.     string getName()
  56.     {
  57.         cout << "得到员工姓名" << endl;
  58.         return NULL;
  59.     }
  60.     string getMobile()
  61.     {
  62.         cout << "得到手机号" << endl;
  63.         return NULL;
  64.     }
  65.     string getOName()
  66.     {
  67.         map<int ,string>::iterator l_it;
  68.         l_it = pmp->find(0);

  69.         return l_it->second;
  70.     }
  71.     string getOMobile()
  72.     {
  73.         map<int ,string>::iterator l_it;
  74.         l_it = pmp->find(1);

  75.         return l_it->second;
  76.     }
  77. private:
  78.     OtherInfo oinf;
  79.     map<int ,string> * pmp;
  80. };

  81. /**
  82.  * @brief 适配器,为了本公司能够搞到其他公司的员工信息!
  83.  * 感觉就是在原来的基础上封了一层???
  84.  */
  85. class Adapter : public CommonUser
  86. {
  87. public:
  88.     string getName();
  89.     string getMobile();
  90. private:
  91.     Function fun;
  92. };


  93. #endif /* _ADAPTER_H_ */
adapter.cpp

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : adapter.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Copyright (c) 2012 Tiros
  6. // Description : adapter in C++, Ansi-style
  7. //============================================================================
  8. #include "adapter.h"

  9. map<int, string> * OtherInfo::getBasicInfo()
  10. {
  11.     map<int ,string> * mp = new map<int ,string>;
  12.     mp->insert(pair<int, string>(0, "hl"));
  13.     mp->insert(pair<int, string>(1, "110"));

  14.     return mp;
  15. }

  16. string UserInfo::getName()
  17. {
  18.     cout << "得到员工姓名" << endl;
  19.     return NULL;
  20. }

  21. string UserInfo::getMobile()
  22. {
  23.     cout << "得到手机号" << endl;
  24.     return NULL;
  25. }

  26. string Adapter::getName()
  27. {
  28.     return fun.getOName();
  29. }
  30. string Adapter::getMobile()
  31. {
  32.     return fun.getOMobile();
  33. }

  34. int main()
  35. {
  36.     CommonUser * puser = new Adapter();
  37.     cout << puser->getMobile() << endl;
  38.     cout << puser->getName() << endl;
  39.     delete puser;

  40.     return 0;
  41. }

阅读(1686) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~