什么是可插拔适配器设计模式? [英] What is pluggable adapter design pattern?
问题描述
提前感谢
以下示例符合以下条件:
适配器应该使用与
客户端已知的相同的旧目标界面来支持适配器(它们是不相关的并且具有
个不同的接口)。
此示例使用C ++ 11 lambda实现与委托相同的功能。
#include< iostream>
#include< functional>
// Adaptee 1
struct CoffeMaker {
void Brew(double quantity,double temp)const {
std :: cout< 我正在酿造<<数量<ml咖啡@<温度<度C< std :: endl;
}
};
// Adaptee 2(与Adaptee 2的差异接口)
struct JuiceMaker {
void Squeeze(double quantity)const {
std :: cout<<< ; 我正在制作<<数量<<ml Juice< std :: endl;
}
};
// Target
struct Beverage {
virtual void getBeverage(int quantity)= 0;
};
//适配器
类适配器:public Beverage {
std :: function< void(int)>请求;
public:
适配器(CoffeMaker * cm1){
Request = [cm1](int quantity){
cm1-> Brew(quantity,80);
};
删除cm1;
}
适配器(JuiceMaker * jm1){
Request = [jm1](int quantity){
jm1-> Squeeze(quantity);
};
删除jm1;
}
void getBeverage(int quantity){
请求(数量);
}
};
//客户端
int main(){
适配器adp1(新的CoffeMaker());
adp1.getBeverage(30);
适配器adp2(新的JuiceMaker());
adp2.getBeverage(40);
}
虽然这个例子是基于这个文章,但我的可插拔适配器示例中有一些异议
I am c++ developer and try to understand the design pattern .In structural pattern there is a specific pattern called adapter pattern.some how i understand this pattern from the GOF book.In this pattern ,there is pluggable adapter pattern,which very confusing and not able to understand it .Googled lot but not able to find a satisfactory answer.Can any one explain what is pluggable adapter design pattern ,with a c++ example ? and also the difference between normal adapter pattern and pluggable adapter pattern. Thanks in advance
The below example fulfill this condition:
adapter should support the adaptees(which are unrelated and have different interfaces) using the same old target interface known to the client.
This example uses C++11 lambda to implement the same functionality like delegates.
#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);
}
Though this example is based on the this article but I have some objections in their pluggable adapter example
这篇关于什么是可插拔适配器设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!