常見設計模式的解析和實現(C++)之十六-Strategy模式
阿新 • • 發佈:2018-12-27
作用:
定義一系列的演算法,把它們一個個封裝起來, 並且使它們可相互替換.本模式使得演算法可獨立於使用它的客戶而變化.
UML結構圖:
解析:
簡而言之一句話,Strategy模式是對演算法的封裝.處理一個問題的時候可能有多種演算法,這些演算法的介面(輸入引數,輸出引數等)都是一致的,那麼可以考慮採用Strategy模式對這些演算法進行封裝,在基類中定義一個函式介面就可以了.
實現:
1)Strategy.h
/**//********************************************************************
created: 2006/08/06
filename: Strategy.h
author: 李創
http://www.cppblog.com/converse/
purpose: Strategy模式的演示程式碼
*********************************************************************/
#ifndef STRATEGY_H
#define STRATEGY_H
class Strategy;
class Context
{
public:
Context(Strategy *pStrategy);
~Context();
void ContextInterface();
private:
Strategy* m_pStrategy;
};
class Strategy
{
public:
virtual~Strategy(){}
virtualvoid AlgorithmInterface() =0;
};
class ConcreateStrategyA
: public Strategy
{
public:
virtual~ConcreateStrategyA(){}
virtualvoid AlgorithmInterface();
};
#endif
2)Strategy.cpp
/**//********************************************************************
created: 2006/08/06
filename: Strategy.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Strategy模式的演示程式碼
*********************************************************************/
#include <iostream>
#include "Strategy.h"
Context::Context(Strategy *pStrategy)
: m_pStrategy(pStrategy)
{
}
Context::~Context()
{
delete m_pStrategy;
m_pStrategy = NULL;
}
void Context::ContextInterface()
{
if (NULL != m_pStrategy)
{
m_pStrategy->AlgorithmInterface();
}
}
void ConcreateStrategyA::AlgorithmInterface()
{
std::cout <<"AlgorithmInterface Implemented by ConcreateStrategyA\n";
}
3)Main.cpp
/**//********************************************************************
created: 2006/08/06
filename: Main.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Strategy模式的測試程式碼
*********************************************************************/
#include "Strategy.h"
int main()
{
Strategy* pStrategy =new ConcreateStrategyA();
Context* pContext =new Context(pStrategy);
pContext->ContextInterface();
delete pContext;
return0;
}
定義一系列的演算法,把它們一個個封裝起來, 並且使它們可相互替換.本模式使得演算法可獨立於使用它的客戶而變化.
UML結構圖:
解析:
簡而言之一句話,Strategy模式是對演算法的封裝.處理一個問題的時候可能有多種演算法,這些演算法的介面(輸入引數,輸出引數等)都是一致的,那麼可以考慮採用Strategy模式對這些演算法進行封裝,在基類中定義一個函式介面就可以了.
實現:
1)Strategy.h
/**//********************************************************************
created: 2006/08/06
filename: Strategy.h
http://www.cppblog.com/converse/
purpose: Strategy模式的演示程式碼
*********************************************************************/
#ifndef STRATEGY_H
#define STRATEGY_H
class Strategy;
class Context
{
public:
Context(Strategy *pStrategy);
~Context();
void ContextInterface();
Strategy* m_pStrategy;
};
class Strategy
{
public:
virtual~Strategy(){}
virtualvoid AlgorithmInterface() =0;
};
class ConcreateStrategyA
: public Strategy
{
public:
virtual~ConcreateStrategyA(){}
virtualvoid AlgorithmInterface();
};
#endif
2)Strategy.cpp
/**//********************************************************************
filename: Strategy.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Strategy模式的演示程式碼
*********************************************************************/
#include <iostream>
#include "Strategy.h"
Context::Context(Strategy *pStrategy)
: m_pStrategy(pStrategy)
{
}
Context::~Context()
{
delete m_pStrategy;
m_pStrategy = NULL;
}
void Context::ContextInterface()
{
if (NULL != m_pStrategy)
{
m_pStrategy->AlgorithmInterface();
}
}
void ConcreateStrategyA::AlgorithmInterface()
{
std::cout <<"AlgorithmInterface Implemented by ConcreateStrategyA\n";
}
3)Main.cpp
/**//********************************************************************
created: 2006/08/06
filename: Main.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Strategy模式的測試程式碼
*********************************************************************/
#include "Strategy.h"
int main()
{
Strategy* pStrategy =new ConcreateStrategyA();
Context* pContext =new Context(pStrategy);
pContext->ContextInterface();
delete pContext;
return0;
}