| CODE |
| void doThingActionOnObject(Object x) { switch(x.getType()) { case A: doAThing(x);break; case B: doBThing(x);break; case C: doCThing(x);break; default: doError(); } } |
| CODE |
| void doResizeOnWindow(Window w); void doOpenOnWindow(Window w); void doCloseOnWindow(Window w); void doMoveOnWindow(Window w);etc.. |
| CODE |
| class ThingAction { public: virtual void doThingAction(Object x) = 0; }; class AAction: public ThingAction { public: virtual void doThingAction(Object x) {/* Do somthing with x */} }; class BAction: public ThingAction { public: virtual void doThingAction(Object x) {/* Do somthing with x */} }; class CAction: public ThingAction { public: virtual void doThingAction(Object x) {/* Do somthing with x */} }; |
| CODE |
| void void doThingActionOnObject(Object x) { x.getAction().execute(x); // Much simpler. } |
| CODE |
| void doThingActionOnObject(Object x) { x->doThingAction(); } void Object::doThingAction() { getAction().execute(*this); } |
| CODE |
| int doMaths(char x,int lhs,int rhs) { int result; switch(x) { case '+': result = doAdd(lhs,rhs);break; case '-': result = doSub(lhs,rhs);break; case '*': result = doMul(lhs,rhs);break; case '/': result = doDiv(lhs,rhs);break; default: Error(); } return(result); } |
| CODE |
| int doMaths(char x,int lhs,int rhs) { int result; CalcCommand* command = commands[x]; if (command != NULL) { result = command->execute(lhs,rhs); } else { Error(); } return(result); } |
| CODE |
#include <map> #include <iostream> class CalcCommand { public: virtual int execute(int lhs,int rhs) = 0;}; class doAdd: public CalcCommand { public: virtual int execute(int lhs,int rhs) {return(lhs + rhs);}}; class doSub: public CalcCommand { public: virtual int execute(int lhs,int rhs) {return(lhs - rhs);}}; class doMul: public CalcCommand { public: virtual int execute(int lhs,int rhs) {return(lhs * rhs);}}; class doDiv: public CalcCommand { public: virtual int execute(int lhs,int rhs) {return(lhs / rhs);}}; std::map<char,CalcCommand*> commands; int doMaths(char x,int lhs,int rhs); int main(int argc,char* argv[]) { commands['+'] = new doAdd; commands['-'] = new doSub; commands['*'] = new doMul; commands['/'] = new doDiv; while(true) { std::cout << "Input Expression in the form 5 + 6 <enter>" << std::endl; int lhs; int rhs; char command; std::cin >> lhs >> command >> rhs; std::cout << "Result: " << doMaths(command,lhs,rhs) << std::endl; } /* * As the commands are dynamically created with new. * They need to be deleted. * The obvious way is just to delete each of the commands added above. * This is a bit error prone as when you add a command you must remember to delete it. * A better solution is to make the commands structure somthing that * automitcally destroys its content when going out of scope. * This is left as an exercise. */ return(0); } void Error() {/*Do Error stuff */} int doMaths(char x,int lhs,int rhs) { int result; CalcCommand* command = commands[x]; if (command != NULL) { result = command->execute(lhs,rhs); } else { Error(); } return(result); } |