设计模式—ADAPTER 适配器模式

ADAPTER 适配器模式

在 GoF 的经典著作《设计模式》中指出: Adapter 模式的目的就是将类的接口转换成客户对象需要的接口, Adapter 模式使原本不相容的接口变的相容。也就是说我们有一个可以满足我们需要的对象,但是她的接口却不是象我们所期望的那样,而我们现在所需要的就是创建一个新的接口,让原本的接口能够满足我们的需要。

适用性
1:你想适用一个已经存在的类,而它的接口不符合你的需求;

2:你想创建一个复用的类,改类可以与其它不相关的类或不可预见的类协同工作;

3:你想使用一些已经存在的子类,对象适配器可以适配它的父类接口;

事例代码如下:

#include <iostream>
using namespace std;

class IDrawStrategy {
public:
	virtual void draw() const = 0;
};

class Grapher {
public:
	Grapher(IDrawStrategy* drawStrategy = 0) : _drawStrategy(drawStrategy) {}

public:
	void drawShape() const;
	void setShape(IDrawStrategy* drawStrategy);

protected:
	IDrawStrategy* _drawStrategy;
};

void Grapher::drawShape() const {
	if (_drawStrategy)
		_drawStrategy->draw();
}

void Grapher::setShape(IDrawStrategy* drawStrategy) {
	_drawStrategy = drawStrategy;
}

class IPaint {
public:
	virtual void paint() const = 0;
};

class Triangle : public IPaint {
public:
	void paint() const;
};

void Triangle::paint() const {
	cout << "Draw Triangle" << endl;
}

class Circle : public IPaint {
public:
	void paint() const;
};

void Circle::paint() const {
	cout << "Draw Circle" << endl;
}

class Square : public IPaint {
public:
	void paint() const;
};

void Square::paint() const {
	cout << "Draw Square" << endl;
}

class DrawAdapter : public IDrawStrategy {
public:
	DrawAdapter(IPaint* adaptee = 0) : _adaptee(adaptee) {}
	virtual void draw() const;

protected:
	IPaint* _adaptee;
};

void DrawAdapter::draw() const {
	_adaptee->paint();
}

int main() {
	Grapher grapher(&DrawAdapter(&Triangle()));
	grapher.drawShape();

	grapher.setShape(&DrawAdapter(&Circle()));
	grapher.drawShape();

	grapher.setShape(&DrawAdapter(&Square()));
	grapher.drawShape();

	getchar();
	return 0;
}
事例代码如下:

#include <iostream>
using namespace std;

class IDrawStrategy {
public:
	virtual void draw() const = 0;
};

class Grapher {
public:
	Grapher(IDrawStrategy* drawStrategy = 0) : _drawStrategy(drawStrategy) {}

public:
	void drawShape() const;
	void setShape(IDrawStrategy* drawStrategy);

protected:
	IDrawStrategy* _drawStrategy;
};

void Grapher::drawShape() const {
	if (_drawStrategy)
		_drawStrategy->draw();
}

void Grapher::setShape(IDrawStrategy* drawStrategy) {
	_drawStrategy = drawStrategy;
}

class IPaint {
public:
	virtual void paint() const = 0;
};

class Triangle : public IPaint {
public:
	void paint() const;
};

void Triangle::paint() const {
	cout << "Draw Triangle" << endl;
}

class Circle : public IPaint {
public:
	void paint() const;
};

void Circle::paint() const {
	cout << "Draw Circle" << endl;
}

class Square : public IPaint {
public:
	void paint() const;
};

void Square::paint() const {
	cout << "Draw Square" << endl;
}

template<typename T>
class DrawAdapter : public IDrawStrategy, private T {
public:
	virtual void draw() const;  
};

template<typename T>
void DrawAdapter<T>::draw() const {
	paint();
}

int main() {
	Grapher grapher(&DrawAdapter<Triangle>());
	grapher.drawShape();

	grapher.setShape(&DrawAdapter<Circle>());
	grapher.drawShape();

	grapher.setShape(&DrawAdapter<Square>());
	grapher.drawShape();

	getchar();

	return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值