Refer to the big talk design pattern and some information on the net, write down some simple design patterns, let yourself understand the beauty of these design patterns!
#ifndef operation_h#define operation_h#include<iostream>using namespace Std;class operation{protected:double OpA, Opb;public:bool SetValue (double& N, double& m); virtual double getresult () const=0;}; Class Operationadd:p ublic operation{double getresult () const;}; Class Operationsub:p ublic operation{double getresult () const;}; Class Operationmul:p ublic operation{double getresult () const;}; Class Operationdiv:p ublic operation{double getresult () const;}; Class Operationfactory{public:operation * Creatoperation (char& operate);}; BOOL Operation::setvalue (double &n, double &m) {OpA = N;OPB = M;return true;} Double Operationadd::getresult () const{double Result;result = OpA + opb;return result;} Double Operationsub::getresult () const{double result;result = Opa-opb;return result;} Double Operationmul::getresult () const{double result;result = OpA * Opb;return result; Double Operationdiv::getresult () const{if (OpB = = 0) {cout << "OpB in Operationdiv can ' t is zero.\n"; return 0.00000001;} Double result;result = Opa/opb;return result;} Operation * Operationfactory::creatoperation (char& operate) {operation * Oper = Null;switch (operate) {case (' + '): O per = new Operationadd;break;case ('-'): Oper = new Operationsub;break;case (' * '): Oper = new Operationmul;break;case ('/'): Oper = new Operationdiv;break;default:break;} return Oper;} #endif
#include "operation.h" int main () {double NumA, numb;char op;operation * oper;operationfactory opfa;cout << "please Enter the numbers and operation (A op B) "; while (Cin>>numa&&cin>>op&&cin>>numb) {Oper = Opfa.creatoperation (OP); Oper->setvalue (NUMA, NumB) cout << "The result of" << NumA << op << NumB << "=" << oper->getresult () << endl;cout << "Please enter the numbers and operation (A op B) ";} return 0;}
Design pattern C + + implementation one: Simple engineering mode