C++设计模式-Adapter适配器模式(转)
Adapter适配器模式
作用:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
分为类适配器模式和对象适配器模式。
系统的数据和行为都正确,但接口不符时,我们应该考虑使用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。
想使用一个已经存在的类,但如果它的接口,也就是它的方法和你的要求不相同时,就应该考虑用适配器模式。
比如购买的第三方开发组件,该组件接口与我们自己系统的接口不相同,或者由于某种原因无法直接调用该组件,可以考虑适配器。
UML图如下:
图1:类模式适配器
图2:对象模式适配器
代码如下:
Adapter.h

1 #ifndef _ADAPTER_H_
2 #define _ADAPTER_H_
3
4 //目标接口类,客户需要的接口
5 class Target
6 {
7 public:
8 Target();
9 virtual ~Target();
10 virtual void Request();//定义标准接口
11 };
12
13 //需要适配的类
14 class Adaptee
15 {
16 public:
17 Adaptee();
18 ~Adaptee();
19 void SpecificRequest();
20 };
21
22 //类模式,适配器类,通过public继承获得接口继承的效果,通过private继承获得实现继承的效果
23 class Adapter:public Target,private Adaptee
24 {
25 public:
26 Adapter();
27 ~Adapter();
28 virtual void Request();//实现Target定义的Request接口
29 };
30
31 //对象模式,适配器类,继承Target类,采用组合的方式实现Adaptee的复用
32 class Adapter1:public Target
33 {
34 public:
35 Adapter1(Adaptee* adaptee);
36 Adapter1();
37 ~Adapter1();
38 virtual void Request();//实现Target定义的Request接口
39 private:
40 Adaptee* _adaptee;
41 };
42 #endif

Adapter.cpp

1 #include "Adapter.h"
2 #include <iostream>
3
4 using namespace std;
5
6 Target::Target()
7 {}
8
9 Target::~Target()
10 {}
11
12 void Target::Request()
13 {
14 cout << "Target::Request()" << endl;
15 }
16
17 Adaptee::Adaptee()
18 {
19 }
20
21 Adaptee::~Adaptee()
22 {
23 }
24
25 void Adaptee::SpecificRequest()
26 {
27 cout << "Adaptee::SpecificRequest()" << endl;
28 }
29
30 //类模式的Adapter
31 Adapter::Adapter()
32 {
33 }
34
35 Adapter::~Adapter()
36 {
37 }
38
39 void Adapter::Request()
40 {
41 cout << "Adapter::Request()" << endl;
42 this->SpecificRequest();
43 cout << "----------------------------" <<endl;
44 }
45
46 //对象模式的Adapter
47 Adapter1::Adapter1():_adaptee(new Adaptee)
48 {
49 }
50
51 Adapter1::Adapter1(Adaptee* _adaptee)
52 {
53 this->_adaptee = _adaptee;
54 }
55
56 Adapter1::~Adapter1()
57 {
58 }
59
60 void Adapter1::Request()
61 {
62 cout << "Adapter1::Request()" << endl;
63 this->_adaptee->SpecificRequest();
64 cout << "----------------------------" <<endl;
65 }

main.cpp

1 #include "Adapter.h"
2
3 int main()
4 {
5 //类模式Adapter
6 Target* pTarget = new Adapter();
7 pTarget->Request();
8
9 //对象模式Adapter1
10 Adaptee* ade = new Adaptee();
11 Target* pTarget1= new Adapter1(ade);
12 pTarget1->Request();
13
14 //对象模式Adapter2
15 Target* pTarget2 = new Adapter1();
16 pTarget2->Request();
17
18 return 0;
19 }
C++设计模式-Adapter适配器模式(转)的更多相关文章
- [C# 设计模式] Adapter - 适配器模式(两种)
Adapter - 适配器模式 序 现实生活中,我们常用到适配器. 你当前打开我这篇文章的笔记本电脑,电源的另一边不正连着一块适配器吗? 你平时想将三口插座插进二口插座里面,不也需要一个适配器吗? 整 ...
- C++设计模式-Adapter适配器模式
Adapter适配器模式作用:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 分为类适配器模式和对象适配器模式. 系统的数据和 ...
- 一天一个设计模式——Adapter适配器模式(Wrapper模式)
一.模式说明 在现实生活中,当需要将两种设备连接起来,但是两个设备的接口规范又不一致(比如电脑上只有Type-C接口,但是你的显示器是HDMI接口),这时候就需要一个适配器,适配器一端连接电脑,一端连 ...
- C#设计模式(7)——适配器模式(Adapter Pattern)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- C#设计模式之七适配器模式(Adapter)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题, ...
- 8.3 GOF设计模式二: 适配器模式 Adapter
GOF设计模式二: 适配器模式 Adapter 为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能.原理?复习单实例模式 SingleTon的三个关键点 ...
- C#设计模式之六适配器模式(Adapter Pattern)【结构型】
一.引言 从今天开始我们开始讲[结构型]设计模式,[结构型]设计模式有如下几种:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.[创建型]的设计模式解决的是对象创建的问题,那[ ...
- C#设计模式(7)——适配器模式(Adapter Pattern)(转)
一.引言 在实际的开发过程中,由于应用环境的变化(例如使用语言的变化),我们需要的实现在新的环境中没有现存对象可以满足,但是其他环境却存在这样现存的对象.那么如果将“将现存的对象”在新的环境中进行调用 ...
随机推荐
- 2015-06-02 关于mvc表格点击按钮自动添加一行<tr></tr>
前台代码: @using (Html.BeginForm("ContactPerson", "User", FormMethod.Post, new { @cl ...
- 详解c++指针的指针和指针的引用(转)
http://www.cnblogs.com/li-peng/p/4116349.html
- js判断是否是移动设备登陆网页
var browser = { versions: function () { var u = navigator.userAgent, app = ...
- 再见OI
NOIP2016终章 自己弱还脑残加手残 再见OI 你好高考 你好明天 "没有泪水的日子会轻松很多 但我的心还是会痛" ------------------------------ ...
- mysql 连接空闲超8小时自动断开连接问题(linux)
在mysql配置文件里添加wait_timeout和interactive_timeout两个值 [mysqld] wait_timeout= interactive_timeout= 超时时间,10 ...
- Xformode的坑
http://blog.csdn.net/u010335298/article/details/51983420
- linux dns 配置
今天线上出现一个bug,图片上传失败. 经过排查发现,上传图片接口调用失败,ping 域名提示 unknow host,ping IP正常. 猜想可能是dns的问题.解决过程如下: /etc 下没有 ...
- 正确理解ThreadLocal
想必很多朋友对 ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本文先谈一下对ThreadLocal的理 解,然后根据ThreadLocal类的 ...
- CentOS 7 下的LAMP实现以及基于https的虚拟主机
系统环境:CentOS 7Apache 2.4php 5.4MariaDB 5.5 项目需求:创建3个虚拟主机,分别架设phpMyadmin,wordpress,Discuz其中phpMyadmin提 ...
- public, protected and private inheritance in C++
Get from Stackoverflow. The details can easily understand from the below example. class A { public: ...