关闭

adapter设计模式

标签: 设计模式
55人阅读 评论(0) 收藏 举报
分类:

适配器设计模式

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






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3890次
    • 积分:160
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论