UML图
代码
main.cpp
# include "Strategy.h"
# include "Context.h"
void test ( )
{
Context* pContext = nullptr ;
pContext = new Context ( new StrategyA ( ) ) ;
pContext-> contextInterface ( ) ;
pContext = new Context ( new StrategyB ( ) ) ;
pContext-> contextInterface ( ) ;
pContext = new Context ( new StrategyC ( ) ) ;
pContext-> contextInterface ( ) ;
delete pContext;
pContext = nullptr ;
}
int main ( )
{
test ( ) ;
system ( "pause" ) ;
}
Strategy.h
# pragma once
# include <iostream>
using namespace std;
class Strategy
{
public :
virtual void algorithmInterface ( ) = 0 ;
} ;
class StrategyA : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法A实现" << endl;
}
} ;
class StrategyB : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法B实现" << endl;
}
} ;
class StrategyC : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法C实现" << endl;
}
} ;
Context.h
# pragma once
# include "Strategy.h"
class Context
{
public :
Context ( Strategy* pStrategy) : m_pStrategy ( pStrategy) { }
void contextInterface ( ) ;
private :
Strategy* m_pStrategy{ nullptr } ;
} ;
void Context :: contextInterface ( )
{
m_pStrategy-> algorithmInterface ( ) ;
}
策略模式+简单工厂
优点 :策略模式+简单工厂:可以完全将策略继承体系与用户端完全剥离开来,将策略继承体系完全封装起来,对用户完全不可见。总结 :
类C通过没什么信息含量的枚举作为入参,利用简单工厂生成类A继承体系中的各子类A1、A2、A3。同时,用基类A作为类C的成员变量,接一下刚生成的类A的子类。 类C对外统一暴露一个接口,该接口中,类C的成员变量类A调用继承体系公有对外暴露的接口func()。
main.cpp
# include "StrategyFactory.h"
void test ( )
{
StrategyFactory* pStrategyFactory = nullptr ;
pStrategyFactory = new StrategyFactory ( StrategyType:: eStrategyA) ;
pStrategyFactory-> contextInterface ( ) ;
pStrategyFactory = new StrategyFactory ( StrategyType:: eStrategyB) ;
pStrategyFactory-> contextInterface ( ) ;
pStrategyFactory = new StrategyFactory ( StrategyType:: eStrategyC) ;
pStrategyFactory-> contextInterface ( ) ;
delete pStrategyFactory;
pStrategyFactory = nullptr ;
}
int main ( )
{
test ( ) ;
system ( "pause" ) ;
}
Strategy.h
# pragma once
# include <iostream>
using namespace std;
class Strategy
{
public :
virtual void algorithmInterface ( ) = 0 ;
} ;
class StrategyA : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法A实现" << endl;
}
} ;
class StrategyB : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法B实现" << endl;
}
} ;
class StrategyC : public Strategy
{
public :
virtual void algorithmInterface ( ) override
{
cout << "算法C实现" << endl;
}
} ;
StrategyFactory.h
# pragma once
# include "Strategy.h"
enum StrategyType
{
eStrategyA,
eStrategyB,
eStrategyC
} ;
class StrategyFactory
{
public :
StrategyFactory ( StrategyType nType) ;
~ StrategyFactory ( ) ;
void contextInterface ( ) ;
private :
Strategy* m_pStrategy{ nullptr } ;
} ;
StrategyFactory :: StrategyFactory ( StrategyType nType)
{
switch ( nType)
{
case eStrategyA:
m_pStrategy = new StrategyA ( ) ;
break ;
case eStrategyB:
m_pStrategy = new StrategyB ( ) ;
break ;
case eStrategyC:
m_pStrategy = new StrategyC ( ) ;
break ;
}
}
StrategyFactory :: ~ StrategyFactory ( )
{
delete m_pStrategy;
m_pStrategy = nullptr ;
}
void StrategyFactory :: contextInterface ( )
{
m_pStrategy-> algorithmInterface ( ) ;
}