独望枫

人在尘世间,有缘自相见,变化千千万,未开窍,已迷恋
posts - 45, comments - 0, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

适配器模式[Adapter]

Posted on 2016-06-10 16:44 小菜枫 阅读(109) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记
      适配器模式主要是可以使两个原本不兼容的功能/类可以兼容。
      其主要有两种方式:一、类适配器,二、对象适配器
      类适配器是指从类层面就开始设计进行适配,主要实现方式为多重继承。
      对象适配器主要是从实例层面进行适配,主要是实现方式为适配器关联一个被适配的对象(实例)。
      类适配器的UML图:
      
      对象适配器的UML图:
     
      示例代码如下
      类适配器:
      USE类
     
 1 #pragma once
 2 class USE
 3 {
 4 public:
 5     USE();
 6     virtual ~USE();
 7 public:
 8     void methodA();
 9     void methodB();
10 };
     
 1 #include "USE.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 
 6 USE::USE()
 7 {
 8 }
 9 
10 
11 USE::~USE()
12 {
13 }
14 
15 void USE::methodA()
16 {
17     cout << "use methodA" << endl;
18 }
19 
20 void USE::methodB()
21 {
22     cout << "use methodB" << endl;
23 }
      ExistFuncModule类
     
 1 #pragma once
 2 class ExistFuncModule
 3 {
 4 public:
 5     ExistFuncModule();
 6     virtual ~ExistFuncModule();
 7 public:
 8     void FuncA();
 9     void FuncB();
10 };
11 
12 
     
 1 #include "ExistFuncModule.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 
 6 ExistFuncModule::ExistFuncModule()
 7 {
 8 }
 9 
10 
11 ExistFuncModule::~ExistFuncModule()
12 {
13 }
14 
15 void ExistFuncModule::FuncA()
16 {
17     cout << "  existfuncmodule funcA" << endl;
18 }
19 
20 void ExistFuncModule::FuncB()
21 {
22     cout << "  existfuncmodule funcB" << endl;
23 }
      ClassApadter类
     
 1 #pragma once
 2 #include "USE.h"
 3 #include "ExistFuncModule.h"
 4 class ClassApadter :
 5     public USE , public ExistFuncModule
 6 {
 7 public:
 8     ClassApadter();
 9     virtual ~ClassApadter();
10 public:
11     void methodA();
12     void methodB();
13 };
14 
15 
     
 1 #include "ClassApadter.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 
 6 ClassApadter::ClassApadter()
 7 {
 8 }
 9 
10 
11 ClassApadter::~ClassApadter()
12 {
13 }
14 
15 void ClassApadter::methodA()
16 {
17     cout << "ClassApadter methodA" << endl;
18     FuncA();
19 }
20 
21 void ClassApadter::methodB()
22 {
23     cout << "ClassApadter methodB" << endl;
24     FuncB();
25 }
26 
     
      对象适配器:
      USE类和ExistFuncModule类和上面一致
      ObjectApadter类
     
 1 #pragma once
 2 #include "USE.h"
 3 class ExistFuncModule;
 4 class ObjectApadter :
 5     public USE
 6 {
 7 public:
 8     ObjectApadter();
 9     virtual ~ObjectApadter();
10 public:
11     void methodA();
12     void methodB();
13 private:
14     ExistFuncModule* pExistFuncModule;
15 };
16 
17 
     
 1 #include "ObjectApadter.h"
 2 #include "ExistFuncModule.h"
 3 #include <iostream>
 4 using namespace std;
 5 
 6 ObjectApadter::ObjectApadter()
 7 {
 8 }
 9 
10 
11 ObjectApadter::~ObjectApadter()
12 {
13 }
14 
15 void ObjectApadter::methodA()
16 {
17     cout << "ObjectApadter methodA" << endl;
18     pExistFuncModule->FuncA();
19 }
20 
21 void ObjectApadter::methodB()
22 {
23     cout << "ObjectApadter methodB" << endl;
24     pExistFuncModule->FuncB();
25 }
     
      以上纯粹为个人学习记录,如有不对,烦请各位大大批评指出……

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理