

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The implementation of the template method pattern in a software design project, specifically in the context of expressions. The motivation behind using the pattern, the implementation details, and the benefits. The case study involves the development of a binaryexpr class and its derived classes for sum, product, and difference expressions.
Typology: Study Guides, Projects, Research
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















class BinaryExpr : public Expr { protected: const Expr* left; const Expr* right; BinaryExpr( const Expr* l, const Expr *r) : leftOperand(l), rightOperand(r) {} };
int BinaryExpr::evaluate(int & v, const Store & st) { int leftval, rightval; if (left->evaluate(leftval,st) && right->evaluate(rightval,st)) { if (type==SUM) v=leftval+rightval; if (type==PRODUCT) v=leftval*rightval; if (type==DIFFERENCE) v=leftval-rightval; return true; } return false; }
int BinaryExpr::evaluate(int & v, const Store & st) { int leftval, rightval; if (left->evaluate(leftval,st) && right->evaluate(rightval,st)) { v=operation(leftval,rightval); return true; } return false; }
int BinaryExpr::evaluate(int & v, const Store & st) { int leftval, rightval; if (left->evaluate(leftval,st) && right->evaluate(rightval,st)) { v=operation(leftval,rightval); return true; } return false; }
const Expr * Product::simplify() const { int leftv, rightv; const Expr leftsimp=left->simplify(); const Expr rightsimp=right->simplify(); bool leftb=leftsimp->evaluate(leftv); bool rightb=rightsimp->evaluate(rightv); if (leftb && rightb) { // simplify constant expressions delete leftsimp; delete rightsimp; if (leftvrightv>=0) return new Literal(leftvrightv); else return new Negation(new Literal(-(leftv*rightv)); } else if (leftb && leftv==1) { delete leftsimp; return rightsimp; } else if (rightb && rightv==1) { delete rightsimp; return leftsimp; } if (leftb && leftv==0 || rightb && rightv==0) { delete leftsimp; delete rightsimp; return new Literal(0); } return new Product(l,r); }
const Expr * BinaryExpr::simplify() const { int leftv, rightv; const Expr * leftsimp=left->simplify(); const Expr * rightsimp=right->simplify(); bool leftb=leftsimp->evaluate(leftv); bool rightb=rightsimp->evaluate(rightv); if (leftb && rightb) { // simplify constant expressions delete leftsimp; delete rightsimp; int val=operation(leftv,rightv); if (val>=0) return new Literal(val); else return new Negation(new Literal(val)); }
else if (leftb && leftidentity(leftv)) { delete leftsimp; return rightsimp; } else if (rightb && rightidentity(rightv)) { delete rightsimp; return leftsimp; }
const Expr * BinaryExpr::simplify() const { int leftv,rightv; const Expr * leftsimp=left->simplify(); const Expr * rightsimp=right->simplify(); bool leftb=leftsimp->evaluate(leftv); bool rightb=rightsimp->evaluate(rightv); if (leftb && rightb) { delete leftsimp; delete rightsimp; int val=operation(leftv,rightv); if (val>=0) return new Literal(val); else return new Negation(new Literal(val)); } else if (leftb && leftidentity(leftv)) { delete leftsimp; return rightsimp; } else if (rightb && rightidentity(rightv)) { delete rightsimp; return leftsimp; } else return othersimplify(leftsimp, rightsimp, leftb, rightb, leftv, rightv);