适配器模式
来源:互联网 发布:淘宝开店类目如何选择 编辑:程序博客网 时间:2024/05/28 15:12
适配器模式有两种:1.类的适配器 2.对象适配器,对象适配器更多一些,这个模式使得复用旧的接口成为可能。
UML示意图
1)采用继承原有接口类的方式
2)采用组合原有接口类的方式
第二种实现方法代码:
1)Adapt.h
#ifndef ADAPTER_H
#define ADAPTER_H
// 需要被Adapt的类
class Target
{
public:
Target(){}
virtual ~Target() {}
virtual void Request() = 0;
};
// 与被Adapt对象提供不兼容接口的类
class Adaptee
{
public:
Adaptee(){}
~Adaptee(){}
void SpecialRequest();
};
// 进行Adapt的类,采用聚合原有接口类的方式
class Adapter
: public Target
{
public:
Adapter(Adaptee* pAdaptee);
virtual ~Adapter();
virtual void Request();
private:
Adaptee* m_pAdptee;
};
#endif
2)Adapt.cpp#include "Adapter.h"
#include <iostream>
void Adaptee::SpecialRequest()
{
std::cout << "SpecialRequest of Adapteen";
}
Adapter::Adapter(Adaptee* pAdaptee)
: m_pAdptee(pAdaptee)
{
}
Adapter::~Adapter()
{
delete m_pAdptee;
m_pAdptee = NULL;
}
void Adapter::Request()
{
std::cout << "Request of Adaptern";
m_pAdptee->SpecialRequest();
}
//Main.cpp
#include "Adapter.h"
#include <stdlib.h>
int main()
{
Adaptee *pAdaptee = new Adaptee;
Target *pTarget = new Adapter(pAdaptee);
pTarget->Request();
delete pTarget;
system("pause");
return 0;
}
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 浅析Java中CountDownLatch用法
- WeUI
- 洛谷 1072
- 奥运奖牌中文系统(IO版)
- DOM事件学习笔记
- 适配器模式
- Android通过Get方法获取Json数据
- java NIO Buffer常用方法
- AJAX学习笔记
- JUC (Java Util Concurrency) 基础内容概述
- HDU - 2516 取石子游戏(斐波那契)
- 我的第一个Go网络程序,有点不完美
- jQuery学习笔记
- Trafodion 数据加载介绍