适配器设计模式

将一个类的接口转换成客户希望的另外一个接口。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设计模式的更多相关文章

  1. 设计模式之Adapter设计模式

    这个设计模式是我这两天刚学的,这儿算是我的读书笔记发布出来是供大家一起学习,后面有我自己的感悟,下面是我网上整理的 以下情况使用适配器模式 • 你想使用一个已经存在的类,而它的接口不符合你的需求. • ...

  2. 设计模式之Adapter模式

    说起Adapter,STL里的stack和queue都是adapter,底层是deque,隐藏了deque的一些接口,使得其可以达到FIFO是queue,LIFO是stack. The STL sta ...

  3. 【设计模式】Adapter

    前言 Adapter设计模式,允许客户端使用接口不兼容的类. 昨天收拾一些以前的东西,发现了藏在柜子里的一条线,这条线叫做OTG.这条线的一端是micro-usb的输出口,另一端是usb的输入口.这条 ...

  4. 北风设计模式课程---接口分离原则(Interface Segregation Principle)

    北风设计模式课程---接口分离原则(Interface Segregation Principle) 一.总结 一句话总结: 接口分离原则描述为 "客户类不应被强迫依赖那些它们不需要的接口& ...

  5. 接口分离原则(Interface Segregation Principle)

    接口分离原则(Interface Segregation Principle)用于处理胖接口(fat interface)所带来的问题.如果类的接口定义暴露了过多的行为,则说明这个类的接口定义内聚程度 ...

  6. 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)

    3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...

  7. Java项目经验——程序员成长的关键(转载)

    Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3.掌握用例驱动.以 ...

  8. C++面向对象设计

    一. 组合(复合),继承,委托 1.composition(组合)has-a 1.1 组合举例:(Adapter 设计模式) 关系: 利用deque功能实现所有queue功能 template < ...

  9. Java项目经验

    Java项目经验 转自CSDN. Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点:1.掌握项目开发的基本步骤2.具备极强的面向对象的 ...

随机推荐

  1. Eclipse Missing artifact jdk.tools:jdk.tools:jar:1.6

    Missing artifact jdk.tools:jdk.tools:jar:1.6 问题出在Eclipse Maven的支持上,在Eclipse下,java.home变量设置为用于启动Eclip ...

  2. nginx-413

    修改nginx配置文件:client_max_body_size 300M 修改php.ini upload_max_filesize | post_max_size

  3. UML指南系列——用例图

    可以用用例来描述正在开发的系统想要实现的行为,而不必说明这些行为如何实现. 结构良好的用例只表示系统或者子系统的基本行为,而且既不过于笼统也不过于详细.

  4. Javascript中的相等比较

    在比较相等或不相等之前,会对操作数进行类型转换,然后比较相等性 在转换不同的数据类型时,相等和不相等操作符遵循下列基本规则: 1.如果由一个操作数是布尔值,则在比较相等性之前先将其转换为数值:2.如果 ...

  5. python-面向对象中的特殊方法 ,反射,与单例模式

    二,面相对象的特殊成员及相关内置函数 2.1 isinstance与issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class A: pass cl ...

  6. zabbix--自带模板监控MySQL服务

    zabbix有内置的模板,不过需要修改下配置,配置文件路径在: [root@localhost ~]# cd /etc/zabbix/ [root@localhost zabbix]# ls web ...

  7. 【Movie】绿皮书

    今天和室友一起去看了<绿皮书>,一部获得奥斯卡的电影. 起初我是没什么太大期望的,纯粹是因为特价票10块钱,加上身边一个小姐姐的力荐. 看完我觉得,啊不愧是奥斯卡电影啊.推荐. 以下可能会 ...

  8. android中的文件存储

    Context类中提供的IO方法: FileOutputSream openFileOutput(String name, int mode):打开应用程序中的/data/data/<应用程序的 ...

  9. js数组去重练习

  10. Asp.net Controller中View 和Action方法认证Authorize 及对AuthorizeAttribute扩展

    Asp.net Controller中View和Action方法认证Authorize 在建立Web 站点安全性时 1.登录后才可访问系统文件 ——限制 Forms认证 <authenticat ...