adapter设计模式
适配器设计模式
将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
例如:现在有一个220V的插口,而手机不能直接接上去,因为锂电池充电的最大电压为5V,这时就需要一个适配器,这个适配器可以接到220V上又能输出5V的电压。在这个例子中,手机是Target,220V的接口是Adaptee(被适配的),充电器头即为适配器
adapter模式的实现方式:
A)类实现(采用多继承,使得适配器继承于Target 和Adaptee)关键词:继承
PS:Java不支持多继承,但可以实现多接口
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// The "Target" class
// (the interface we need)
class Compound
{
protected:
string _chemical;
float _boilingPoint;
float _meltingPoint;
double _molecularWeight;
string _molecularFormula;
public:
Compound(string chemical)
{
this->_chemical = chemical;
}
virtual void Display()
{
cout << "\nCompound: " << _chemical << " ------ \n";
}
};
// assistant function
void toLower(string& s)
{
for (int i = 0; i < s.length(); ++i)
s[i] = tolower(s[i]);
}
// The 'Adaptee' class
// (the old interface we want to adapt)
class ChemicalDatabank
{
public:
float GetCriticalPoint(string compound, string point)
{
toLower(compound);
// Melting Point
if (point == "M")
{
if (compound == "water") return 0.0f;
if (compound == "benzene") return 5.5f;
if (compound == "ethanol") return -114.1f;
return 0.0f;
}
// Boiling Point
else
{
if (compound == "water") return 100.0f;
if (compound == "benzene") return 80.1f;
if (compound == "ethanol") return 78.3f;
return 0.0f;
}
}
string GetMolecularStructure(string compound)
{
toLower(compound);
if (compound == "water") return "H20";
if (compound == "benzene") return "C6H6";
if (compound == "ethanol") return "C2H5OH";
return "";
}
double GetMolecularWeight(string compound)
{
toLower(compound);
if (compound == "water") return 18.015;
if (compound == "benzene") return 78.1134;
if (compound == "ethanol") return 46.0688;
return 0.0;
}
};
// The 'Adapter' class
// (the implementation of the interface)
class RichCompound : public Compound
{
private:
ChemicalDatabank* _bank;
// Constructor
public:
RichCompound(string name)
: Compound(name)
{
_bank = new ChemicalDatabank();
_boilingPoint = _bank->GetCriticalPoint(_chemical, "B");
_meltingPoint = _bank->GetCriticalPoint(_chemical, "M");
_molecularWeight = _bank->GetMolecularWeight(_chemical);
_molecularFormula = _bank->GetMolecularStructure(_chemical);
}
virtual void Display()
{
Compound::Display();
cout << " Formula: " << _molecularFormula << endl;
cout << " Weight : " << _molecularWeight << endl;
cout << " Melting Pt: " << _meltingPoint << endl;
cout << " Boiling Pt: " << _boilingPoint << endl;
}
};
int main()
{
// Non-adapted chemical compound
Compound* unknown = new Compound("Unknown");
unknown->Display();
// Adapted chemical compounds
Compound* water = new RichCompound("Water");
water->Display();
Compound* benzene = new RichCompound("Benzene");
benzene->Display();
Compound* ethanol = new RichCompound("Ethanol");
ethanol->Display();
// Wait for user
cout << "\nPress Enter key to finish...";
cin.ignore(100, '\n');
}
B)对象实现:(适配器继承于Target,但有一个Adaptee(被适配)类对象成员) -------关键词:对象的组合
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// The "Target" class
// (the interface we need)
class Compound
{
protected:
string _chemical;
float _boilingPoint;
float _meltingPoint;
double _molecularWeight;
string _molecularFormula;
public:
Compound(string chemical)
{
this->_chemical = chemical;
}
virtual void Display()
{
cout << "\nCompound: " << _chemical << " ------ \n";
}
};
// assistant function
void toLower(string& s)
{
for (int i = 0; i < s.length(); ++i)
s[i] = tolower(s[i]);//把大写字符转化为小写
}
// The 'Adaptee' class
// (the old interface we want to adapt)
class ChemicalDatabank
{
public:
float GetCriticalPoint(string compound, string point)
{
toLower(compound);
// Melting Point
if (point == "M")
{
if (compound == "water") return 0.0f;
if (compound == "benzene") return 5.5f;
if (compound == "ethanol") return -114.1f;
return 0.0f;
}
// Boiling Point
else
{
if (compound == "water") return 100.0f;
if (compound == "benzene") return 80.1f;
if (compound == "ethanol") return 78.3f;
return 0.0f;
}
}
string GetMolecularStructure(string compound)
{
toLower(compound);
if (compound == "water") return "H20";
if (compound == "benzene") return "C6H6";
if (compound == "ethanol") return "C2H5OH";
return "";
}
double GetMolecularWeight(string compound)
{
toLower(compound);
if (compound == "water") return 18.015;
if (compound == "benzene") return 78.1134;
if (compound == "ethanol") return 46.0688;
return 0.0;
}
};
// The 'Adapter' class
// (the implementation of the interface)
class RichCompound : public Compound,public ChemicalDatabank //修改
{
private:
//ChemicalDatabank* _bank; 删除
// Constructor
public:
RichCompound(string name)
: Compound(name)
{
//删除_bank = new ChemicalDatabank();
//修改下述代码
_boilingPoint = GetCriticalPoint(_chemical, "B");
_meltingPoint = GetCriticalPoint(_chemical, "M");
_molecularWeight = GetMolecularWeight(_chemical);
_molecularFormula = GetMolecularStructure(_chemical);
}
virtual void Display()
{
Compound::Display();
cout << " Formula: " << _molecularFormula << endl;
cout << " Weight : " << _molecularWeight << endl;
cout << " Melting Pt: " << _meltingPoint << endl;
cout << " Boiling Pt: " << _boilingPoint << endl;
}
};
int main()
{
// Non-adapted chemical compound
Compound* unknown = new Compound("Unknown");
unknown->Display();
// Adapted chemical compounds
Compound* water = new RichCompound("Water");
water->Display();
Compound* benzene = new RichCompound("Benzene");
benzene->Display();
Compound* ethanol = new RichCompound("Ethanol");
ethanol->Display();
// Wait for user
cout << "\nPress Enter key to finish...";
cin.ignore(100, '\n');
}
Proxy(代理)设计模式
为其他对象提供一种代理以控制对这个对象的访问。
代理模式中一般有3中对象:
抽象角色:声明真实对象和代理对象的共同接口。
代理角色:代理对象角色内部含有对真实对象的引用,从而可以操作真实对象,同时代理对象提供与真实对象相同的接口以便在任何时刻都能代替真实对象。同时,代理对象可以在执行真实对象操作时,附加其他的操作,相当于对真实对象进行封装。
真实角色:代理角色所代表的真实对象,是我们最终要引用的对象。
例如:有一家公司创立了一个品牌A化肥,生产核心产品,同时有区域代理商专门负责对该产品进行加工使其有某一特性,这时“卖品牌A的人”为抽象对象,该公司为真实角色,代理商为代理角色。proxy模式有一个重要的特性:对修改关闭,对拓展开放。换句话说:当某客户有新的特性需求,需要A产品发生改变,这时就需要改变A的生产配方,但是如果在该总公司这一层改的话,其余的地区可能不想要这种特性,解决方法:在该客户所在区域代理商加工过程中改变,即还是有总公司运来的产品但改变加工方式。
运用代理的好处:客户不与厂家直接接触,降低耦合,客户只需要直接找代理商即可
什么情况下运用proxy模式:
一个对象,比如一幅很大的图像,需要载入的时间很长。
一个需要很长时间才可以完成的计算结果,并且需要在它计算过程中显示中间结果
一个存在于远程计算机上的对象,需要通过网络载入这个远程对象则需要很长时间,特别是在网络传输高峰期。
一个对象只有有限的访问权限,代理模式(Proxy)可以验证用户的权限
adapter设计模式的更多相关文章
- 设计模式之Adapter设计模式
这个设计模式是我这两天刚学的,这儿算是我的读书笔记发布出来是供大家一起学习,后面有我自己的感悟,下面是我网上整理的 以下情况使用适配器模式 • 你想使用一个已经存在的类,而它的接口不符合你的需求. • ...
- 设计模式之Adapter模式
说起Adapter,STL里的stack和queue都是adapter,底层是deque,隐藏了deque的一些接口,使得其可以达到FIFO是queue,LIFO是stack. The STL sta ...
- 【设计模式】Adapter
前言 Adapter设计模式,允许客户端使用接口不兼容的类. 昨天收拾一些以前的东西,发现了藏在柜子里的一条线,这条线叫做OTG.这条线的一端是micro-usb的输出口,另一端是usb的输入口.这条 ...
- 北风设计模式课程---接口分离原则(Interface Segregation Principle)
北风设计模式课程---接口分离原则(Interface Segregation Principle) 一.总结 一句话总结: 接口分离原则描述为 "客户类不应被强迫依赖那些它们不需要的接口& ...
- 接口分离原则(Interface Segregation Principle)
接口分离原则(Interface Segregation Principle)用于处理胖接口(fat interface)所带来的问题.如果类的接口定义暴露了过多的行为,则说明这个类的接口定义内聚程度 ...
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...
- Java项目经验——程序员成长的关键(转载)
Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3.掌握用例驱动.以 ...
- C++面向对象设计
一. 组合(复合),继承,委托 1.composition(组合)has-a 1.1 组合举例:(Adapter 设计模式) 关系: 利用deque功能实现所有queue功能 template < ...
- Java项目经验
Java项目经验 转自CSDN. Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的 ...
随机推荐
- promise的基本用法
// Promise 对象,可以保存状态 //Promise 第一步 // 异步代码 写在 Promise的函数中 第二步 const promise = new Promise((resolve, ...
- python_面向对象,类名称空间,对象名称空间,组合
创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类有两种属性:静态属性和动态属性 静态属性就是直接在类中定义的变量(字段) 动态属性就是定义在类中的方法 其中类 ...
- JQ实现仿淘宝条件筛选
首先看下效果: Js代码: <script type="text/javascript"> $(".search_qxxx > ul > li & ...
- shell变量替换扩展 字符串计数截取
- linux常用命令记录(一)
文件搜索命令 grep在文件中查找字符并输出 grep 字符或字符串 文件目录 grep pub /teach/.txt -c 字符出现总行数 grep .txt -n 行号 grep .txt -i ...
- BZOJ 3546 [ONTAK2010]Life of the Party (二分图最大匹配必须点)
题解:给出一个二分图,问你取点哪个点会使得二分图匹配数减少. 解法:其实就是问二分图匹配的必须点.先对初始二分图做一次最大匹配. 现在考虑左边点,看残余网络上的边重新构图:如果是匹配边那么就从右往左连 ...
- OpenCV的安装与配置
1.去官网下载opencv,在本教程中选用的时opencv3.4.1,其他版本的配置方法异曲同工.下载链接http://opencv.org/releases.html,选择sources版本 2.解 ...
- js 单行间隙滚动
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- python 对redis key的基本操作
首先看一下Python 操作redis.StrictRedis 的初始化方法__init__ def __init__(self, host='localhost', port=6379, db=0, ...
- centos 6.5 修改主机名
使用 root 账号登录,编辑 vi /etc/sysconfig/network 此时的主机名如下 HOSTNAME=localhost.localdomain 修改 HOSTNAME=newnam ...