未名
5毛党党员
全部博文(17)
2011年(1)
2009年(16)
anank
7717060
flw
HonestQi
scutan
cugb_cat
浅尝寂寞
gong_yue
分类: C/C++
2009-02-24 16:44:55
#include <string> #include <iostream> using namespace std; class Strategy { public: virtual void AlgorithmInterface(){} }; class ConcreateStrategyA : Strategy { public: virtual void AlgorithmInterface() { cout<<"算法A实现"<<endl; } }; class ConcreateStrategyB : Strategy { public: virtual void AlgorithmInterface() { cout<<"算法B实现"<<endl; } }; class ConcreateStrategyC : Strategy { public: virtual void AlgorithmInterface() { cout<<"算法C实现"<<endl; } }; class Context { private: Strategy* m_strategy; public: Context(Strategy* strategy) { this->m_strategy = strategy; } void ContextInterface() { m_strategy->AlgorithmInterface(); } }; class SignContext { private: Strategy* m_strategy; public: SignContext(char Sign) { switch (Sign) { case 'A': m_strategy = (Strategy*)new ConcreateStrategyA();; break; case 'B': m_strategy = (Strategy*)new ConcreateStrategyB(); break; case 'C': m_strategy = (Strategy*)new ConcreateStrategyC(); break; default: ; } } void showResult(void) { m_strategy->AlgorithmInterface(); } }; int main(void) { SignContext* sc = new SignContext('B'); sc->showResult(); };
上一篇:装饰模式(Decorator)_c++
下一篇:建造者模式(Builder)_c++
登录 注册