适配器设计模式

将一个类的接口转换成客户希望的另外一个接口。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)可以验证用户的权限

最新文章

  1. C++模板编程:如何使非通用的模板函数实现声明和定义分离
  2. 关于databinding的细节
  3. 二叉平衡查找树AvlTree(C实现)
  4. JAVA 中BIO,NIO,AIO的理解
  5. Homework3
  6. [FJSC2014]化合物
  7. 在docker以FPM-PHP运行php,慢日志导致的BUG分析
  8. 30款javascript脚本插件 jquery插件大全
  9. HDU 1934 树状数组 也可以用线段树
  10. 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
  11. .Net文件压缩
  12. 37 【kubernetes】搭建dashboard
  13. Linux 针对nginx日志文件做ip防刷限制
  14. 理解iOS与函数式编程
  15. hdu3790最短路径问题(BFS+优先队列)
  16. 因为smb和nfs挂掉导致客户端开机启动不了
  17. for程序员:这些你可能遇到的职场难题,我们帮你整理好了答案
  18. Linux常用的指令(...编辑文件+保存)
  19. Python代码规范总结
  20. USE [EPPM] [dbo].[REFRDEL_CLEANUP]

热门文章

  1. vue input聚焦时,滚动至可视区域
  2. ARC080E
  3. Inversion of Control 控制反转 有什么好处
  4. 2019-5-21-win10-uwp-url-encode
  5. 关于 群晖 docker 百度云盘下载的使用心得
  6. 微信小程序分享功能的path路径
  7. java笔试题大全之IO流常见选择题
  8. java中String类的面试题大全含答案
  9. Centos7.2安装MariaDB数据库,并进行基础配置
  10. Android 为点击事件添加震动效果