定义一系列算法类,当算法变化时,不影响客户端.
适用场景:类中的成员以方法为主,算法经常变动.
好处:每种算法都有自己的类,可以通过自己的接口单独测试,简化了单元测试.
缺点:客户端需要判断适用何种算法.
// coperation.h
#ifndef COPERATION_H
#define COPERATION_H
class COperation
{
public:
COperation();
public:
int m_nFirst;
int m_nSecond;
virtual double GetResult();
};
#endif // COPERATION_H
// coperation.cpp
#include "coperation.h"
COperation::COperation()
{
}
double COperation::GetResult()
{
double dResult = 0;
return dResult;
}
// caddoperation.h
#ifndef CADDOPERATION_H
#define CADDOPERATION_H
#include "coperation.h"
class CAddOperation : public COperation
{
public:
CAddOperation();
CAddOperation(int a, int b);
virtual double GetResult();
};
#endif // CADDOPERATION_H
// caddoperation.cpp
#include "caddoperation.h"
CAddOperation::CAddOperation()
{
}
double CAddOperation::GetResult()
{
return m_nFirst + m_nSecond;
}
CAddOperation::CAddOperation(int a, int b)
{
m_nFirst = a;
m_nSecond = b;
}
// csuboperation.h
#ifndef CSUBOPERATION_H
#define CSUBOPERATION_H
#include "coperation.h"
class CSubOperation : public COperation
{
public:
CSubOperation();
CSubOperation(int a, int b);
virtual double GetResult();
};
#endif // CSUBOPERATION_H
// csuboperation.cpp
#include "csuboperation.h"
CSubOperation::CSubOperation()
{
}
double CSubOperation::GetResult()
{
return m_nFirst - m_nSecond;
}
CSubOperation::CSubOperation(int a, int b)
{
m_nFirst = a;
m_nSecond = b;
}
// context.h
#ifndef CONTEXT_H
#define CONTEXT_H
#include "coperation.h"
class Context
{
public:
Context();
private:
COperation *op;
public:
Context(COperation *temp);
double GetResult();
};
#endif // CONTEXT_H
// context.cpp
#include "context.h"
Context::Context()
{
}
Context::Context(COperation *temp)
{
op = temp;
}
double Context::GetResult()
{
return op->GetResult();
}
客户端(需要知道具体算法类, 并且做判断):
// main.cpp
#include <iostream>
#include "context.h"
#include "caddoperation.h"
#include "csuboperation.h"
using namespace std;
int main(int argc, char *argv[])
{
int a,b;
char c;
cin >> a;
cin >> b;
cin >> c;
switch (c) {
case '+':
{
Context *context = new Context(new CAddOperation(a, b));
cout << context->GetResult() << endl;
}
break;
case '-':
{
Context *context = new Context(new CSubOperation(a, b));
cout << context->GetResult() << endl;
}
break;
default:
break;
}
return 0;
}
策略模式与工厂模式结合:
// context_with_factory.h
#ifndef CONTEXT_WITH_FACTORY_H
#define CONTEXT_WITH_FACTORY_H
#include "coperation.h"
class Context_with_factory
{
public:
Context_with_factory();
Context_with_factory(char c, int a, int b);
virtual double GetResult();
private:
COperation *op;
};
#endif // CONTEXT_WITH_FACTORY_H
// context_with_factory.cpp
#include "context_with_factory.h"
#include "caddoperation.h"
#include "csuboperation.h"
Context_with_factory::Context_with_factory()
{
}
Context_with_factory::Context_with_factory(char c, int a, int b)
{
switch (c) {
case '+':
{
op = new CAddOperation(a, b);
}
break;
case '-':
{
op = new CSubOperation(a, b);
}
break;
default:
{
op = nullptr;
}
break;
}
}
double Context_with_factory::GetResult()
{
return op->GetResult();
}
客户端:
// main.cpp
#include <iostream>
#include "context_with_factory.h"
using namespace std;
int main(int argc, char *argv[])
{
int a, b;
cin >> a;
cin >> b;
Context_with_factory *context_with_factory = new Context_with_factory('+', a, b);
cout << context_with_factory->GetResult() << endl;
return 0;
}