Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41365
  • 博文数量: 17
  • 博客积分: 1432
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-27 09:38
文章分类

全部博文(17)

文章存档

2011年(1)

2009年(16)

最近访客

分类: 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();
};

阅读(791) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~