码迷,mamicode.com
首页 > 编程语言 > 详细

用C++学习设计模式之 简单工厂模式

时间:2015-02-08 12:55:51      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:工厂模式

</pre>拿到了《大话设计模式》这本书,从这本书的风格来看,感觉这本书还是非常适合大学教育的。但是对于初学者来讲,C#是不太容易接受的。为了解决这个问题,同时为了提升自己的C++水平以及加深对面向对象的理解,笔者决定用C++学习一下这本书。<p></p><p><span style="white-space: pre;">运算类头文件</span></p><p><span style="white-space: pre;"></span></p><pre name="code" class="cpp">#ifndef OPERATION_H
#define OPERATION_H


class Operation
{
    public:
        Operation();
        virtual ~Operation();
        void set_numberA(double _numberA);
        void set_numberB(double _numberB);
        double get_numberA();
        double get_numberB();
        double virtual  get_result();
    protected:
        double numberA;
        double numberB;
    private:
};

#endif // OPERATION_H

运算类.cpp文件

#include "Operation.h"

Operation::Operation()
{
    //ctor
    numberA = 0;
    numberB = 0;
}

Operation::~Operation()
{
    //dtor
}

void Operation::set_numberA(double _numberA){
    numberA = _numberA;
}
void Operation::set_numberB(double _numberB){
    numberB = _numberB;
}

double Operation::get_numberA(){
    return this->numberA;
}
double Operation::get_numberB(){
    return this->numberB;
}
double   Operation::get_result(){
    return 0.0;
}

然后是+、-、*、/四个类分别继承运算类

#ifndef OPERATIONADD_H
#define OPERATIONADD_H
#include"Operation.h"

class OperationAdd:public Operation
{
    public:
        double get_result();
    protected:
    private:
};

#endif // OPERATIONADD_H
#include "OperationAdd.h"

double OperationAdd::get_result(){
    double result = 0;
    result = numberA + numberB;
    return result;
}


#ifndef OPERATIONSUB_H
#define OPERATIONSUB_H
#include"Operation.h"

class OperationSub:public Operation
{
    public:
        double get_result();
    protected:
    private:
};

#endif // OPERATIONSUB_H

#include "OperationSub.h"

double OperationSub::get_result(){
    double result = 0;
    result = numberA - numberB;
    return result;
}



#ifndef OPERATIONMUL_H
#define OPERATIONMUL_H
#include"Operation.h"

class OperationMul:public Operation
{
    public:
        double get_result();
    protected:
    private:
};

#endif // OPERATIONMUL_H

#include "OperationMul.h"

double OperationMul::get_result(){
    double result = 0;
    result = numberA*numberB;
    return result;
}


#ifndef OPERATIONDIV_H
#define OPERATIONDIV_H
#include"Operation.h"

class OperationDiv:public Operation
{
    public:
        double get_result();
    protected:
    private:
};

#endif // OPERATIONDIV_H

#include "OperationDiv.h"

double OperationDiv::get_result(){
    double result = 0;
    result = numberA/numberB;
    return result;
}



然后是一个工厂类

#ifndef OPERATIONFACTORY_H
#define OPERATIONFACTORY_H
#include"Operation.h"
#include"OperationAdd.h"
#include"OperationSub.h"
#include"OperationMul.h"
#include"OperationDiv.h"

class OperationFactory
{
    public:
        static Operation *CreateOperation(char ch);
        OperationFactory();
        virtual ~OperationFactory();
    protected:
    private:
};

#endif // OPERATIONFACTORY_H

#include "OperationFactory.h"

OperationFactory::OperationFactory()
{
    //ctor
}

OperationFactory::~OperationFactory()
{
    //dtor
}
Operation *OperationFactory::CreateOperation(char ch){
    Operation *oper;
    switch(ch){
    case '+':
        oper = new OperationAdd();
        break;
    case '-':
        oper = new OperationSub();
        break;
    case '*':
        oper = new OperationMul();
        break;
    case '/':
        oper = new OperationDiv();
        break;

    }
    return oper;
}


最后是主类

#include <iostream>
#include"Operation.h"
#include"OperationAdd.h"
#include"OperationSub.h"
#include"OperationMul.h"
#include"OperationDiv.h"
#include"OperationFactory.h"
using namespace std;

int main()
{
    Operation* oper;
    oper = OperationFactory::CreateOperation('*');
    oper->set_numberA(1);
    oper->set_numberB(2);
    cout<<oper->get_result();
    return 0;
}



用C++学习设计模式之 简单工厂模式

标签:工厂模式

原文地址:http://blog.csdn.net/u014293306/article/details/43636137

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!