1,设计模式C++学习笔记之一(Strategy策略模式)

时间:2021-08-24 21:58:36

因为最近在学习设计模式,网上有些人介绍的设计模式发现还是不好容易理解。所以找到了这个博客中的设计模式发现还是很容易学习和理解的。http://www.cnblogs.com/wanggary/archive/2011/04/07/2008796.html

1.1.解释

main(),赵云

CContext,锦囊

IStrategy,策略接口

CBackDoor,策略之一

CGivenGreenLight,策略之二

CBlockEnemy,策略之三

说明:一个策略放到一个锦囊里。当用的时候,找到这个锦囊,从锦囊里拿出策略来使用。

注意:锦囊只是简单的装载和调用策略,锦囊里没有逻辑。策略会有更大的自主权,运行更多的逻辑。

看代码:

//Context.h

#pragma once
#include "IStrategy.h"

class CContext
{
public:
    CContext(IStrategy *pStrategy);
    ~CContext(void);
    void Operate(void);
private:
    IStrategy *m_pStrategy;
};

//Context.cpp

#include "StdAfx.h"
#include "Context.h"

CContext::CContext(IStrategy *pStrategy)
{
    this->m_pStrategy = pStrategy;
}
CContext::~CContext(void)
{
    delete this->m_pStrategy;
}
void CContext::Operate(void)
{
    this->m_pStrategy->Operate();
}

//IStrategy.h

#pragma once

class IStrategy
{
public:
    IStrategy(void){}
    virtual ~IStrategy(void){}
    virtual void Operate(void) = 0;
};

//BackDoor.h

#pragma once
#include "IStrategy.h"
class CBackDoor :
    public IStrategy
{
public:
    CBackDoor(void);
    ~CBackDoor(void);
    void Operate(void);
};

//BackDoor.cpp

#include "StdAfx.h"
#include "BackDoor.h"
#include <iostream>
using std::cout;
using std::endl;
CBackDoor::CBackDoor(void)
{
}
CBackDoor::~CBackDoor(void)
{
}
void CBackDoor::Operate(void)
{
    cout << "找乔国老帮忙,让吴国太给孙权施加压力" << endl;
}

//GivenGreenLight.h

#pragma once
#include "IStrategy.h"
class CGivenGreenLight :
    public IStrategy
{
public:
    CGivenGreenLight(void);
    ~CGivenGreenLight(void);
    void Operate(void);
};

//GivenGreenList.cpp

#include "StdAfx.h"
#include "GivenGreenLight.h"
#include <iostream>
using std::cout;
using std::endl;
CGivenGreenLight::CGivenGreenLight(void)
{
}
CGivenGreenLight::~CGivenGreenLight(void)
{
}
void CGivenGreenLight::Operate(void)
{
    cout << "求吴国太开个绿灯,放行!" << endl;
}

//BlockEnemy.h

#pragma once
#include "IStrategy.h"
class CBlockEnemy :
    public IStrategy
{
public:
    CBlockEnemy(void);
    ~CBlockEnemy(void);
    void Operate(void);
};

//BlockEnemy.cpp

#include "StdAfx.h"
#include "BlockEnemy.h"
#include <iostream>
using std::cout;
using std::endl;
CBlockEnemy::CBlockEnemy(void)
{
}
CBlockEnemy::~CBlockEnemy(void)
{
}
void CBlockEnemy::Operate()
{
    cout << "孙夫人断后,挡住追兵" << endl;
}

//Strategy.cpp

#include "stdafx.h"
#include "Context.h"
#include "BackDoor.h"
#include "GivenGreenLight.h"
#include "BlockEnemy.h"
#include <iostream>
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
    CContext *pContext;

    cout << "----------刚刚到吴国的时候拆第一个----------" << endl;
    pContext = new CContext(new CBackDoor());
    pContext->Operate();
    delete pContext;

    cout << "----------刘备乐不思蜀了,拆第二个了----------" << endl;
    pContext = new CContext(new CGivenGreenLight());
    pContext->Operate();
    delete pContext;

    cout << "----------孙权的小兵追了,咋办?拆第三个----------" << endl;
    pContext = new CContext(new CBlockEnemy());
    pContext->Operate();
    delete pContext;

    return 0;
}

这运行上述代码的过程中发现:error LNK2019: 无法解析的外部符号 "public: __thiscall IStrategy::IStrategy(void)" (??0IStrategy@@QAE@XZ),该符号在函数 "public: __thiscall CBackDoor::CBackDoor(void)" (??0CBackDoor@@QAE@XZ) 中被引用

编译器有报这个错误。我的开发环境是VS2010。查找网上可知:IStrategy.h中的纯虚函数的构造和析构函数没有函数体导致的