c++ 设计模式之 策略模式与工厂模式结合

#include
using namespace std;

class COperation
{
public:
int m_first;
int m_second;
virtual double getResult()
{
return 0;
}
};
class addCOperation: public COperation
{
public:
virtual double getResult()
{
return m_first + m_second;
}
};

//
class Context
{
public:
COperation* m_op;
public:
Context(COperation *p)
{
m_op = p;
}
};

int main()
{
Context *op = new Context(new addCOperation);
op->m_op->m_first = 1;
op->m_op->m_second = 2;
cout<m_op->getResult()< return 0;
}

你可能感兴趣的:(设计模式,c++,设计模式,工厂模式,策略模式)