我是c++开发人员,试图理解.In结构模式的设计模式有一个特定的模式叫做适配器pattern.some我是如何从GOF book.In这个模式中理解这个模式的,有可插拔适配器模式,它非常混乱,并且不能理解它.Googled很多但却找不到一个满意的answer.Can谁来解释一下什么是可插拔适配器设计模式,用一个c++例子?以及普通适配器模式和可插拔适配器模式之间的区别。提前感谢
发布于 2017-01-13 06:25:56
以下示例满足此条件:
适配器应该支持使用客户端已知的相同旧目标接口的适配器(它们不相关,并且具有不同的接口)。
此示例使用C++11 lambda来实现与委托相同的功能。
#include <iostream>
#include <functional>
//Adaptee 1
struct CoffeMaker {
void Brew (double quantity, double temp) const{
std::cout << "I am brewing " << quantity <<"ml coffee @" << temp <<" degree C" <<std::endl;
}
};
//Adaptee 2 (having difference interface from Adaptee 2)
struct JuiceMaker{
void Squeeze (double quantity) const{
std::cout << "I am making " << quantity <<"ml Juice" <<std::endl;
}
};
// Target
struct Beverage{
virtual void getBeverage (int quantity) = 0;
};
// Adapter
class Adapter : public Beverage{
std::function<void(int)> Request;
public:
Adapter(CoffeMaker *cm1){
Request = [cm1](int quantity){
cm1->Brew(quantity,80);
};
delete cm1;
}
Adapter(JuiceMaker *jm1){
Request = [jm1](int quantity){
jm1->Squeeze(quantity);
};
delete jm1;
}
void getBeverage(int quantity){
Request(quantity);
}
};
//Client
int main(){
Adapter adp1(new CoffeMaker());
adp1.getBeverage(30);
Adapter adp2(new JuiceMaker());
adp2.getBeverage(40);
}虽然这个示例是基于this article的,但是我对他们的可插拔适配器示例有一些异议
发布于 2014-11-30 15:08:30
请检查此问题的答案是否令您满意。我认为插拔是最好的解释了:Can anybody explain the concept of pluggable adapter to me with good example?
https://stackoverflow.com/questions/27210898
复制相似问题