Design Patterns----简单的工厂模式
实例:
实现一个简单的计算器。实现加减乘除等操作。。
operator.h 文件
// copyright @ L.J.SHOU Mar.13, 2014
// a simple calculator using Factory Design Pattern
#ifndef OPERATOR_H_
#define OPERATOR_H_ #include <string>
#include <iostream>
#include <stdexcept> // base class
class Operator
{
public:
Operator(double lhs, double rhs)
: numberA(lhs), numberB(rhs){} virtual double GetResult() = 0; protected:
double numberA;
double numberB;
}; // "+"
class OperatorAdd : public Operator
{
public:
OperatorAdd(double lhs, double rhs)
: Operator(lhs, rhs) { } double GetResult() { return numberA + numberB; }
}; // "-"
class OperatorMinus : public Operator
{
public:
OperatorMinus(double lhs, double rhs)
: Operator(lhs, rhs) { } double GetResult() { return numberA - numberB; }
}; // "*"
class OperatorMul : public Operator
{
public:
OperatorMul(double lhs, double rhs)
: Operator(lhs, rhs) { } double GetResult() { return numberA * numberB; }
}; // "/"
class OperatorDiv : public Operator
{
public:
OperatorDiv(double lhs, double rhs)
: Operator(lhs, rhs) { } double GetResult() {
if(numberB == 0)
throw std::runtime_error("divide zero !!!");
return numberA / numberB;
}
}; // factory function
Operator* createOperator(std::string oper, double lhs, double rhs)
{
Operator* pOper(NULL); if(oper == "+")
{
pOper = new OperatorAdd(lhs, rhs);
}
else if(oper == "-")
{
pOper = new OperatorMinus(lhs, rhs);
}
else if(oper == "*")
{
pOper = new OperatorMul(lhs, rhs);
}
else if(oper == "/")
{
pOper = new OperatorDiv(lhs, rhs);
}
else
{
std::cerr << "not a valid operator" << std::endl;
return NULL;
} return pOper;
} #endif
operator.cc 文件
// copyright @ L.J.SHOU Mar.13, 2014
// a simple calculator using Factory Design Pattern #include "operator.h"
#include <iostream>
#include <stdexcept>
#include "boost/shared_ptr.hpp"
using namespace std; int main(void)
{
try{
boost::shared_ptr<Operator> pOper(createOperator("+", 0, 1));
cout << pOper->GetResult() << endl; pOper = boost::shared_ptr<Operator>(createOperator("-", 0, 1));
cout << pOper->GetResult() << endl; pOper = boost::shared_ptr<Operator>(createOperator("*", 2, 3));
cout << pOper->GetResult() << endl; pOper = boost::shared_ptr<Operator>(createOperator("/", 1, 0));
cout << pOper->GetResult() << endl;
}
catch(std::runtime_error err){
std::cout << err.what()
<< std::endl;
} return 0;
}
参考:
大话设计模式
Design Patterns----简单的工厂模式的更多相关文章
- Java EE设计模式(主要简单介绍工厂模式,适配器模式和模板方法模式)
Java EE设计模式分为三种类型,共23种: 创建型模式:单例模式.抽象工厂模式.建造者模式.工厂模式.原型模式. 结构型模式:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式 ...
- js简单的工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- JS模式:又一个简单的工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- JS创建对象的四种简单方式 (工厂模式和自定义构造函数创建对象的区别)
// 对象:特指的某个事物,具有属性和方法(一组无序的属性的集合) // 特征------>属性 // 行为------>方法 // 创建对象的四种方式 1 // 1.字面量的方式,就是实 ...
- java简单的工厂模式
定义:专门定义一个类来创建其他类的实例,被创建的实例通常都具有共同的父类和接口.意图:提供一个类由它负责根据一定的条件创建某一及具体类的实例 //简单工厂,存在不符合开闭原则的地方,可以在参考抽象工厂 ...
- C#调用短信接口(通过简单的工厂模式整合多个短信平台)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- c++ 设计模式之简单的工厂模式
调试环境:vs2010 // test0.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...
- [Java反射机制]用反射改进简单工厂模式设计
如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...
- java之设计模式工厂三兄弟之简单工厂模式
[学习难度:★★☆☆☆,使用频率:★★★☆☆] 工厂模式是最常用的一类创建型设计模式,通常我们所说的工厂模式是指工厂方法模式,它也是使用频率最高的工厂模式.本章将要学习的简单工厂模式是工厂方法模式的& ...
随机推荐
- uva----(100)The 3n + 1 problem
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- Wilcoxon test
clear load NPSVOR name={'SCV1V1','SVC1VA','SVR','CSSVC','SVMOP','NNOP','ELMOP','POM',... 'NNPOM', 'S ...
- Aeroplane chess(HDU 4405)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)
假设有一个基于.Net的Web Service,其名称为SaveProduct POST /ProductService.asmx HTTP/1.1 Host: localhost Content-T ...
- git 将本地项目添加到远程
git init git add README.md git commit -m "first commit" git remote add origin git@github.c ...
- java.io.FileOutputStream类的5个构造方法
java.io.FileOutputStream的构造函数: ①FileOutputStream(File file) ②FileOutputStream(String name) ③FileOutp ...
- Nginx介绍
原文:http://www.aosabook.org/en/nginx.html 作者: Andrew Alexeev nginx(发音"engine x")是俄罗斯软件工程师Ig ...
- SQL查询表占用空间大小
SQL查询表占用空间大小. create table tmp (name varchar(50),rows int,reserved varchar(50),data varchar(50),inde ...
- validator
http://rickharrison.github.io/validate.js/validate.js rules: 'required|callback_check_password' vali ...
- CSS 垂直居中。
1,display: table; display: table-cell <div style="border:solid red 1px ;height:200px;width:2 ...