josansun / designpattern Goto Github PK
View Code? Open in Web Editor NEWThis is a repository for examples of Design Pattern using C++
This is a repository for examples of Design Pattern using C++
`// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
class Oper
{
public:
double getNumberA()
{
return x;
}
void setNumberA(double xx)
{
x = xx;
}
double getNumberB()
{
return y;
}
void setNumberB(double yy)
{
y = yy;
}
virtual double getResult()//使oper可实例化
{
return 0.0;
}
private:
double x;
double y;
};
class OperatorAdd :public Oper
{
public:
double getResult()
{
return getNumberA() + getNumberB();
}
};
class OperatorSub :public Oper
{
public:
double getResult()
{
return getNumberA() - getNumberB();
}
};
class OperatorMul :public Oper
{
public:
double getResult()
{
return getNumberA() * getNumberB();
}
};
class OperatorDiv :public Oper
{
public:
double getResult()
{
try
{
if (fabs(getNumberB()) < 1e-9)
{
throw overflow_error("Divide by zero exception");
}
}
catch (overflow_error& e)
{
cout << e.what() << " -> " << getNumberB() << endl;
}
return getNumberA() / getNumberB();
}
};
class OperFactory
{
public:
static Oper* createOper(char operChar)
{
//这边会报错
Oper oper;
switch (operChar)
{//这里原先用法不对,应将实例的类名放在模版中,结果显示正确
case '+':
oper = new OperatorAdd();
break;
case '-':
oper = new OperatorSub();
break;
case '':
oper = new OperatorMul();
break;
case '/':
oper = new OperatorDiv();
break;
//other operation
default:
oper = NULL;
}
return oper;
}
};
void testSimpleFactoryMode()
{
shared_ptr oper(OperFactory::createOper('+'));
oper->setNumberA(1);
oper->setNumberB(2);
double res = oper->getResult();
cout << res << endl;
}
int main()
{
testSimpleFactoryMode();
return 0;
}
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.