zoukankan      html  css  js  c++  java
  • 设计模式复习-简单工厂模式

                                        简单工厂模式


    实现整数四则运算 (1) 基本实现  (2) 健壮实现  (3) 工厂模式

    #pragma once
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    //(1)基本实现
    void BasicFun() {
        int nNumberFirst, nNumberSecond;
        char cSymbol;
        cin >> nNumberFirst;
        cin >> nNumberSecond;
        cin >> cSymbol;
        if (cSymbol == '+') cout << nNumberFirst + nNumberSecond;
        if (cSymbol == '-') cout << nNumberFirst - nNumberSecond;
        if (cSymbol == '*')cout << nNumberFirst * nNumberSecond;
        if (cSymbol == '/')cout << nNumberFirst / nNumberSecond;
        getchar();
        return ;
    }
    //(2)规范和健壮性实现
    void StandardAndRobustFun() {
        int  nNumberFirst, nNumberSecond;
        string strSymbol="";
        try {
            cin >> nNumberFirst;
            cin >> nNumberSecond;
            cin >> strSymbol;
            if (nNumberSecond == 0 && strSymbol[0] == '/') {
                cout << "error:The dividend cannot be zero ";
                return;
            }
            switch (strSymbol[0]) {
            case '+': cout << nNumberFirst + nNumberSecond << endl; break;
            case '-': cout << nNumberFirst - nNumberSecond << endl; break;
            case '*': cout << nNumberFirst * nNumberSecond << endl; break;
            case '/': cout << nNumberFirst / nNumberSecond << endl; break;
            default:cout << "Input Format Error" << endl; break;
            }
        }
        catch (void *pErrorKey) {
            cout << "Unknow Error" << endl;
        }
        return;
    }
    //(3)简单工厂模式
    class COperationClass {
    public:
        virtual bool GetValue(const int &nNumberFirst, const int &nNumberSecond, int &nNumberAns) = 0;
    };
    class CAddition :public COperationClass {
    public:
        bool GetValue(const int &nNumberFirst, const int &nNumberSecond, int &nNumberAns) {
            nNumberAns = nNumberFirst + nNumberSecond;
            return true;
        }
    };
    class Csubtraction :public COperationClass {
    public:
        bool GetValue(const int &nNumberFirst, const int &nNumberSecond, int &nNumberAns) {
            nNumberAns = nNumberFirst - nNumberSecond;
            return true;
        }
    };
    class Cmultiplication :public COperationClass {
    public:
        bool GetValue(const int &nNumberFirst, const int &nNumberSecond, int &nNumberAns) {
            nNumberAns = nNumberFirst * nNumberSecond;
            return true;
        }
    };
    
    class Cdivision :public COperationClass {
    public:
        bool GetValue(const int &nNumberFirst, const int &nNumberSecond, int &nNumberAns) {
            if (nNumberSecond == 0) {
                return 0;
            }
            nNumberAns = nNumberFirst / nNumberSecond;
            return true;
        }
    };
    
    class CFactory {
    public:
        bool GetAlgorithm(const string &strSymbol, COperationClass * &pAlgorithm) {
            pAlgorithm = NULL;
            switch (strSymbol[0]) {
            case '+': pAlgorithm = new CAddition(); break;
            case '-': pAlgorithm = new Csubtraction(); break;
            case '*': pAlgorithm = new Cmultiplication(); break;
            case '/': pAlgorithm = new Cdivision(); break;
            default: return false;
            }
            return pAlgorithm != NULL;
        }
    };
    
    int main()
    {
        system("color 0b");
        //BasicFun();
        //StandardAndRobustFun();
        CFactory CFactorTest;
        COperationClass * pOperationNow = NULL;
        if (CFactorTest.GetAlgorithm("+", pOperationNow)) {
            int nComputerResult = 0;
            pOperationNow->GetValue(1, 2, nComputerResult);
            cout << nComputerResult << endl;
            delete(pOperationNow);
        }
        else {
            cout << "error" << endl;
        }
        getchar();
        return 0;
    }
  • 相关阅读:
    Windows莫名内存到百分之百,需要修改虚拟内存
    idea中的springboot的maven项目报错Failed to clean project: Failed to delete D: ew_shunyishunyi argetshunyiWEB-INFclassesstatic
    VMware 启动虚拟机黑屏(Ubuntu)
    MySQL 8.0.18安装教程(windows 64位)
    ubunt中,使用命令su命令切换root账户,提示认证失败
    AngularJS 杂项知识点
    AngularJS $watch 性能杀手
    AngularJS controller as vm方式
    AngularJS 路由 resolve属性
    AngularJS $observe $watch
  • 原文地址:https://www.cnblogs.com/csnd/p/12061939.html
Copyright © 2011-2022 走看看