voidDuck::peformfly(){
this->action.fly();
}
#include<iostream>
#include<string>
classStratery{
public:
Stratery(){
}
virtualvoidAlgorithmInterface(){
std::cout<<"AlgorithmInterface"<<std::endl;
}
};
classConcreteStrateryA:publicStratery{
public:
virtualvoidAlgorithmInterface(){
std::cout<<"ConcreteStrateryA"<<std::endl;
}
};
classConcreteStrateryB:publicStratery{
public:
virtualvoidAlgorithmInterface(){
std::cout<<"ConcreteStrateryB"<<std::endl;
}
};
classConcreteStrateryC:publicStratery{
public:
virtualvoidAlgorithmInterface(){
std::cout<<"ConcreteStrateryC"<<std::endl;
}
};
classContext{
private:
Stratery*stratery;
public:
Context(Stratery*_stratery):stratery(_stratery){
}
voidContextInterface(){
stratery->AlgorithmInterface();
}
};
int main(){
Context contextA(newConcreteStrateryA());
contextA.ContextInterface();
Context contextB(newConcreteStrateryB());
contextB.ContextInterface();
Context contextC(newConcreteStrateryB());
contextC.ContextInterface();
}
ConcreteStrateryA
ConcreteStrateryB
ConcreteStrateryC
原文:http://www.cnblogs.com/yml435/p/6926373.html