Created
November 14, 2012 00:10
-
-
Save grodtron/4069296 to your computer and use it in GitHub Desktop.
Simple calculator program to demonstrate the factory pattern in cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.swp | |
*.swo | |
*.o | |
test | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Addition.h" | |
Addition::Addition(){ | |
setFirstOperand(0); | |
setSecondOperand(0); | |
} | |
Addition::~Addition(){ | |
} | |
double Addition::getResult(){ | |
return firstOperand + secondOperand; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ADDITION_H_ | |
#define ADDITION_H_ | |
#include "MathOperation.h" | |
class Addition : public MathOperation { | |
public: | |
Addition(); | |
virtual ~Addition(); | |
// definition of pure virtual method from superclass | |
virtual double getResult(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Division.h" | |
Division::Division(){ | |
setFirstOperand(0); | |
setSecondOperand(1); // so we don't divide by zero | |
} | |
Division::~Division(){ | |
} | |
void Division::setSecondOperand(double x){ | |
// we do some validation to make sure that we don't divide by zero, | |
// and then call the sueprclass function | |
if (x != 0) MathOperation::setSecondOperand(x); | |
} | |
double Division::getResult(){ | |
return firstOperand / secondOperand; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DIVISION_H_ | |
#define DIVISION_H_ | |
#include "MathOperation.h" | |
class Division : public MathOperation { | |
public: | |
Division(); | |
virtual ~Division(); | |
virtual void setSecondOperand(double); | |
// definition of pure virtual method from superclass | |
virtual double getResult(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#include "MathOperation.h" | |
#include "MathOperationFactory.h" | |
int main(int argc, const char *argv[]) | |
{ | |
char op; | |
double first, second; | |
// Get parameters to pass to the factory and the resulting object | |
cout << "Enter a simple math expression (e.g. '1 + 2' or '3.14159 * 25'): "; | |
cin >> first >> op >> second; | |
// create some kind of operation object based on the user input | |
MathOperation * operation = MathOperationFactory::createMathOperation(op); | |
// configure it | |
operation->setFirstOperand(first); | |
operation->setSecondOperand(second); | |
// get the result | |
cout << "Result is: " << operation->getResult() << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: clean all | |
all: test | |
%.o: %.cpp | |
g++ -c $< -o $@ | |
test: $(shell ls -1 *.cpp | sed 's/\.cpp/\.o/') | |
g++ $^ -o $@ | |
clean: | |
rm -f *.o | |
rm -f test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MATH_OPERATION_H_ | |
#define MATH_OPERATION_H_ | |
// An abstract class representing the idea of a mathematical operation on | |
// two operands (for example: addition, subtraction, multiplication or division) | |
class MathOperation { | |
protected: | |
// data members | |
double firstOperand; | |
double secondOperand; | |
public: | |
// some simple setters to allow configuring the operation. | |
virtual void setFirstOperand(double x){ firstOperand = x; } | |
virtual void setSecondOperand(double x){ secondOperand = x; } | |
// pure virtual method that should be implemented in subclasses | |
virtual double getResult() = 0; | |
// virtual destructor, since we intend to subclass this class | |
virtual ~MathOperation(){} | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MathOperationFactory.h" | |
#include "Addition.h" | |
#include "Subtraction.h" | |
#include "Multiplication.h" | |
#include "Division.h" | |
MathOperation * MathOperationFactory::createMathOperation(char operation){ | |
switch(operation){ | |
case '+': | |
return new Addition(); | |
case '-': | |
return new Subtraction(); | |
case '*': | |
return new Multiplication(); | |
case '/': | |
return new Division(); | |
default: | |
return new Addition(); // <-- we'll arbitrarily make this the default | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MATH_OPERATION_FACTORY_H_ | |
#define MATH_OPERATION_FACTORY_H_ | |
#include "MathOperation.h" | |
class MathOperationFactory { | |
public: | |
// the static factory method | |
static MathOperation * createMathOperation(char); | |
private: | |
MathOperationFactory(){} // no publicly accessible ctor | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Multiplication.h" | |
Multiplication::Multiplication(){ | |
setFirstOperand(0); | |
setSecondOperand(0); | |
} | |
Multiplication::~Multiplication(){ | |
} | |
double Multiplication::getResult(){ | |
return firstOperand * secondOperand; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MULTIPLICATION_H_ | |
#define MULTIPLICATION_H_ | |
#include "MathOperation.h" | |
class Multiplication : public MathOperation { | |
public: | |
Multiplication(); | |
virtual ~Multiplication(); | |
// definition of pure virtual method from superclass | |
virtual double getResult(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Subtraction.h" | |
Subtraction::Subtraction(){ | |
setFirstOperand(0); | |
setSecondOperand(0); | |
} | |
Subtraction::~Subtraction(){ | |
} | |
double Subtraction::getResult(){ | |
return firstOperand - secondOperand; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SUBTRACTION_H_ | |
#define SUBTRACTION_H_ | |
#include "MathOperation.h" | |
class Subtraction : public MathOperation { | |
public: | |
Subtraction(); | |
virtual ~Subtraction(); | |
// definition of pure virtual method from superclass | |
virtual double getResult(); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment