C++适配器简单介绍

绑定适配器:

struct Myprint:public binary_function<int,int,void>
{
	void operator()(int v,int val)const
	{
cout << "v:" << v << " " << "val:" << val << " " << "(v + val):" << v + val << endl;
	}
};

void main01()//绑定适配器
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), bind1st(Myprint(), 200));
	//将一个二元函数对象转换成一元函数对象
	//bind1st将200绑定为函数对象的第一个参数
	//bind2nd将200绑定为函数对象的第一个参数
}

仿函数适配器  not1 not2取反适配器:

struct MyCompare :public binary_function<int, int, void>
{
	bool operator()(int val1, int val2)const
	{
		return	val1 < val2;
	}
};

struct Myprint02
{
	void operator()(int v)
	{
		cout << v << ends;
	}
};

struct MyCompare02:public unary_function<int,bool>
{
	bool operator()(int val1)const
	{
		return	val1 > 10;
	}
};

void main02()//仿函数适配器  not1 not2取反适配器
{
	vector<int>v1;
	srand(time(0));
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(rand()%100);
	}
	for_each(v1.begin(), v1.end(), Myprint02()); 
	cout << endl;
	sort(v1.begin(), v1.end(),not2( MyCompare()));
	for_each(v1.begin(), v1.end(), Myprint02());
	cout << endl;
	vector<int>::iterator it = find_if(v1.begin(), v1.end(), not1(MyCompare02()));
	for_each(v1.begin(), v1.end(), Myprint02());
	if (it==v1.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << endl;
		cout << *it << endl;
	}
	//如果对一元谓词取反,用not1
	//如果对二元谓词取反,用not2
}

ptr_fun函数对象适配器:

void myprint(int val,int val2)
{
	cout << "val:" << val <<" "<< "val2:" << val2 << endl;
	cout << val + val2 << endl;
}


void main03()//ptr_fun把普通函数,转换成函数对象
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(rand() % 100);
	}
	for_each(v1.begin(), v1.end(),bind2nd(ptr_fun(myprint),10));
}

成员函数适配器 mem_fun、mem_fun_ref:

void main04()
{
	//如果容器中存放对象或指针,我们要打印的时候
	vector<My>v1;
	My p(10, 10), p1(20, 20), p2(30, 30), p3(40, 40);
	v1.push_back(p);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	//格式:&类名::函数名
	for_each(v1.begin(), v1.end(), mem_fun_ref(&My::show));
	cout << endl;
	vector<My*>v2;
	v2.push_back(&p);
	v2.push_back(&p1);
	v2.push_back(&p2);
	v2.push_back(&p3);
	for_each(v2.begin(), v2.end(), mem_fun(&My::show));
}

 

### 适配器模式的概念与作用 适配器模式(Adapter Pattern)是一种结构型设计模式,其主要目的是使两个不兼容的接口能够一起工作[^1]。通过创建一个适配器类,可以将一个类的接口转换成客户端期望的另一个接口,从而使原本因接口不匹配而无法一起工作的类能够协同工作。 ### C++适配器模式的实现方式 适配器模式可以通过两种主要方式实现:**类适配器**和**对象适配器**。 #### 类适配器适配器通过多重继承实现适配功能。以下是一个简单的类适配器示例: ```cpp #pragma once #include <iostream> using namespace std; // 被适配者类 class Adaptee { public: void SpecificRequest() { cout << "Called SpecificRequest()" << endl; } }; // 目标接口类 class Target { public: virtual void Request() = 0; // 定义目标接口 }; // 适配器类 class Adapter : public Target, public Adaptee { public: void Request() override { // 实现目标接口 SpecificRequest(); // 调用被适配者的特定方法 } }; ``` 在上述代码中,`Adapter` 类同时继承了 `Target` 和 `Adaptee`,并通过重写 `Request` 方法调用了 `Adaptee` 的 `SpecificRequest` 方法。 #### 对象适配器 对象适配器通过组合的方式实现适配功能。这种方式更加灵活,避免了多重继承可能带来的复杂性。 ```cpp #pragma once #include <iostream> using namespace std; // 被适配者类 class Adaptee { public: void SpecificRequest() { cout << "Called SpecificRequest()" << endl; } }; // 目标接口类 class Target { public: virtual void Request() = 0; // 定义目标接口 }; // 适配器类 class Adapter : public Target { private: Adaptee* adaptee; // 组合被适配者 public: Adapter(Adaptee* adaptee_) : adaptee(adaptee_) {} void Request() override { // 实现目标接口 adaptee->SpecificRequest(); // 调用被适配者的特定方法 } }; ``` 在对象适配器中,`Adapter` 类通过组合 `Adaptee` 实现了适配功能,而不是直接继承 `Adaptee`[^2]。 ### 适配器模式的使用场景 适配器模式适用于以下场景: - 当需要使用已有的类,但其接口不符合需求时。 - 当希望复用一些现存的类,但这些类的接口不统一时。 - 当希望定义一个可以重复使用的类,用于与其他不相关的类或不可预见的类一起工作时。 例如,在实际开发中,适配器模式常用于将不同的硬件驱动程序接口统一为一个标准接口,以便应用程序能够以一致的方式访问不同类型的硬件设备。 ### 示例代码运行说明 假设有一个 `Turkey` 类表示火鸡的行为,而我们需要将其行为适配为鸭子的行为(即实现 `Duck` 接口)。以下是具体的实现: ```cpp #include <iostream> using namespace std; // Duck 接口 class Duck { public: virtual void quack() = 0; // 鸭子的叫声 virtual void fly() = 0; // 鸭子的飞行行为 }; // Turkey 类 class Turkey { public: virtual void gobble() { cout << "嘎嘎嘎" << endl; } virtual void fly() { cout << "I am flying a short distance" << endl; } }; // TurkeyAdapter 类 class TurkeyAdapter : public Duck { private: Turkey* turkey; public: TurkeyAdapter(Turkey* turkey_) : turkey(turkey_) {} void quack() override { // 将火鸡的叫声适配为鸭子的叫声 turkey->gobble(); } void fly() override { // 将火鸡的飞行行为适配为鸭子的飞行行为 for (int i = 0; i < 5; ++i) { turkey->fly(); } } }; int main() { Turkey* turkey = new Turkey(); Duck* duck = new TurkeyAdapter(turkey); duck->quack(); // 输出: 嘎嘎嘎 duck->fly(); // 输出: I am flying a short distance (连续输出5次) delete turkey; delete duck; return 0; } ``` 在上述代码中,`TurkeyAdapter` 类将 `Turkey` 的行为适配为 `Duck` 的行为,从而实现了不同接口之间的兼容性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值