[C++]适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

github源码路径:https://github.com/dangwei-90/Design-Mode

// 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// 参考大话设计模式 - 适配器模式

// 适配器模式:作为两个不兼容的接口之间的桥梁

#include <iostream>

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
#endif

using namespace std;

// 用例一
class Target {
public:
	void Request() {
		cout << "normal request" << endl;
	}
};

class Adaptee {
public:
	void SpecialRequest() {
		cout << "special request" << endl;
	}
};

class Adapter : public Target {
public:
	~Adapter() {
		SAFE_DELETE(adaptee_);
	}

	void Request() {
		adaptee_->SpecialRequest();
	}

private:
	Adaptee* adaptee_ = new Adaptee();
};

// 用例二
class CPlayer {
public:
	CPlayer() {}

	virtual void attack() = 0;

	virtual void stop() = 0;

public:
	string name_ = "";
};

class CForwards : public CPlayer {
public:
	CForwards(string name) {
		name_ = name;
	}

	void attack() {
		cout << name_ << ", attack!" << endl;
	}

	void stop() {
		cout << name_ << ", stop!" << endl;
	}
};

class CCenters : public CPlayer {
public:
	CCenters(string name) {
		name_ = name;
	}

	void attack() {
		cout << name_ << ", attack!" << endl;
	}

	void stop() {
		cout << name_ << ", stop!" << endl;
	}
};

class CGuards : public CPlayer {
public:
	CGuards(string name) {
		name_ = name;
	}

	void attack() {
		cout << name_ << ", attack!" << endl;
	}

	void stop() {
		cout << name_ << ", stop!" << endl;
	}
};

class ForeignCenter {
public:
	void SetForeignName(string name) {
		name_ = name;
	}

	void attack() {
		cout << name_ << ", attack!" << endl;
	}

	void stop() {
		cout << name_ << ", stop!" << endl;
	}

public:
	string name_;
};

// 适配器模式,用户翻译 ForeignCenter 的方法
class Translator : public CPlayer {
public:
	Translator(string name) {
		foreign_->SetForeignName(name);
	}

	~Translator() {
		SAFE_DELETE(foreign_);
	}

	void attack() {
		foreign_->attack();
	}

	void stop() {
		foreign_->stop();
	}

public:
	ForeignCenter* foreign_ = new ForeignCenter();
};

int main()
{
	// 示例一
	Adapter* adapter = new Adapter();
	adapter->Request();
	SAFE_DELETE(adapter);

	// 示例二
	CPlayer* forward = new CForwards("前锋1号");
	forward->attack();
	forward->stop();
	SAFE_DELETE(forward);

	CPlayer* center = new CCenters("中锋3号");
	center->attack();
	center->stop();
	SAFE_DELETE(center);

	CPlayer* guard = new CGuards("后卫1号");
	guard->attack();
	guard->stop();
	SAFE_DELETE(guard);

	// 通过适配器翻译官,进行翻译,调用对应的方法
	CPlayer* translator = new Translator("姚明");
	translator->attack();
	translator->stop();
	SAFE_DELETE(translator);

	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配器模式是一种结构型设计模式,它允许你将不兼容的对象包装在一个适配器中,以使其与另一个类兼容。 在 C++ 中,适配器模式可以通过类适配器和对象适配器来实现。 类适配器使用多重继承来实现适配器和目标类的接口之间的转换。适配器类继承目标类和适配器接口类,并实现适配器接口类的方法,将目标类的方法转换为适配器接口类的方法。 例如,假设我们有一个 `Target` 类和一个 `Adaptee` 类,它们的接口不兼容。我们可以创建一个 `Adapter` 类,它继承 `Target` 类并实现 `Adaptee` 类的方法,将 `Adaptee` 类的方法转换为 `Target` 类的方法。 ```c++ class Target { public: virtual void request() = 0; }; class Adaptee { public: void specificRequest() { std::cout << "Adaptee specific request" << std::endl; } }; class Adapter : public Target, private Adaptee { public: void request() override { specificRequest(); } }; ``` 对象适配器使用组合来实现适配器和目标类的接口之间的转换。适配器类包含一个目标类的实例,并实现适配器接口类的方法,将目标类的方法转换为适配器接口类的方法。 例如,我们可以创建一个 `Adapter` 类,它包含一个 `Adaptee` 类的实例,并实现 `Target` 类的方法,将 `Target` 类的方法转换为 `Adaptee` 类的方法。 ```c++ class Adapter : public Target { public: Adapter(Adaptee* adaptee) : m_adaptee(adaptee) {} void request() override { m_adaptee->specificRequest(); } private: Adaptee* m_adaptee; }; ``` 使用适配器模式可以使我们在不修改现有代码的情况下,将旧接口转换为新接口,从而使不兼容的类能够协同工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值