适配器设计模式

将一个类的接口转换成客户希望的另外一个接口。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. python学习笔记:文件操作和集合

    一.文件操作 文件读写步骤:有一个文件,打开文件,操作文件读写文件,关闭文件. python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例) 模式 可做操作 若文件不存在 是否覆盖 r ...

  2. -webkit-box 高度自动填满

    <style> .box{ display: -webkit-box; -webkit-box-orient: vertical; height: 200px; background: # ...

  3. 接口调用post请求参数在body中

    package com.ynhrm.common.utils; import com.alibaba.fastjson.JSONObject; import lombok.Data; import o ...

  4. ArcMap属性表操作接口ITableWindow3

    ITableWindow3 tableWindow3 = new TableWindowClass                {                    //Layer = laye ...

  5. axure破解版

    axure 破解版   https://www.cnblogs.com/lianghong/p/9385233.html   授权: zdfans.com 注册码:  gP5uuK2gH+iIVO3Y ...

  6. 微信小程序のwxs语言

    一.wxs介绍 wxs是微信小程序自身的脚本语言,用来过滤和计算.wxs可以通过文件可模块标签来定义 文件需要.wxs后缀文件 二.实例 <wxs module="test1" ...

  7. 2018-3-5-安装-pip

    title author date CreateTime categories 安装 pip lindexi 2018-3-5 19:4:4 +0800 2018-03-05 18:57:15 +08 ...

  8. tee - 从标准输入写往文件和标准输出

    总览 (SYNOPSIS) tee [OPTION]... [FILE]... 描述 (DESCRIPTION) 把 标准输入 的 数据 复制到 每一个 文件 FILE, 同时 送往 标准输出. -a ...

  9. codeforces 724G - Xor-matic Number of the Graph 线性基+图

    题目传送门 题意:给出衣服无向带权图,问有多少对合法的$<u,v,s>$,要求$u$到$v$存在一条路径(不一定是简单路径)权值异或和等于$s$,并且$u<v$.求所有合法三元组的s ...

  10. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...