Reputation: 239
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
Upvotes: 1
Views: 4487
Reputation: 2110
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
Upvotes: 1