I am studying design patterns, I was wondering if my approach makes sense. I am trying to implement the strategy pattern. I think I have captured the essence which is
- Define a family of algorithms,
- encapsulate each one,
- and make them interchangeable,
as shown in the example code below.
Have I captured the intent, am I missing something from the example?
#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
enum OperationType
{
Add = 1, Subtract, Multiply, Divide
};
class Operation
{
public:
Operation(int Value1, int Value2) :
m_Value1(Value1),
m_Value2(Value2) { }
virtual int Calculate() = 0;
private:
int m_Result;
int m_Value1;
int m_Value2;
};
class Addition : public Operation
{
public:
Addition(int Value1, int Value2) : Operation(Value1,Value2),
m_Value1(Value1), m_Value2(Value2) {}
int Calculate()
{
m_Result = m_Value1 + m_Value2;
return m_Result;
}
private:
int m_Result;
int m_Value1;
int m_Value2;
};
class Subtraction : public Operation
{
public:
Subtraction(int Value1, int Value2) : Operation(Value1, Value2),
m_Value1(Value1), m_Value2(Value2) {}
int Calculate()
{
m_Result = m_Value1 - m_Value2;
return m_Result;
}
private:
int m_Result;
int m_Value1;
int m_Value2;
};
class Multiplication : public Operation
{
public:
Multiplication(int Value1, int Value2) : Operation(Value1, Value2),
m_Value1(Value1), m_Value2(Value2) {}
int Calculate()
{
m_Result = m_Value1 * m_Value2;
return m_Result;
}
private:
int m_Result;
int m_Value1;
int m_Value2;
};
class Division : public Operation
{
public:
Division(int Value1, int Value2) : Operation(Value1, Value2),
m_Value1(Value1), m_Value2(Value2) {}
int Calculate()
{
m_Result = m_Value1 / m_Value2;
return m_Result;
}
private:
int m_Result;
int m_Value1;
int m_Value2;
};
class Test
{
public:
Test()
{
m_Operation = NULL;
}
void SetOperation(int Value1, int Value2, int Operation)
{
switch (Operation)
{
case Add:
{
m_Operation = new Addition(Value1, Value2);
}
break;
case Subtract:
{
m_Operation = new Subtraction(Value1, Value2);
}
break;
case Multiply:
{
m_Operation = new Multiplication(Value1, Value2);
}
break;
case Divide:
{
m_Operation = new Division(Value1, Value2);
}
break;
}
}
int PerformOperation()
{
return m_Operation->Calculate();
}
private:
Operation *m_Operation;
};
int main()
{
Test test;
test.SetOperation(1, 2, OperationType::Add);
std::cout << test.PerformOperation() << std::endl;
test.SetOperation(2, 3, OperationType::Subtract);
std::cout << test.PerformOperation() << std::endl;
test.SetOperation(3, 4, OperationType::Multiply);
std::cout << test.PerformOperation() << std::endl;
test.SetOperation(40, 10, OperationType::Divide);
std::cout << test.PerformOperation() << std::endl;
int x;
std::cin >> x;
return 0;
}
cout << 1 + 2 << endl;
and it is very hard to write better code than that. So the question you need to ask yourself is why exactly you need abstraction layers here. Look for the problem to solve, not for the design pattern to use. \$\endgroup\$