• 大话设计模式之简单工厂模式代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 简单工厂模式
    {
        //operation 运算类
        public class operation
        {
            private double _numberA = 0;
            private double _numberB = 0;
            public double NumberA
            {
                get { return _numberA; }
                set { _numberA = value; }
            }
            public double NumberB
            {
                get { return _numberB; }
                set { _numberB = value; }
            }
            public virtual double GetResult()
            {
                double result = 0;
                return result;
            }
        }
        class OperationAdd : operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = NumberA + NumberB;
                return result;
            }
        }
        class OperationSub : operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = NumberA - NumberB;
                return result;
            }
        }
        class OperationMul : operation
        {
            public override double GetResult()
            {
                double result = 0;
                result = NumberA * NumberB;
                return result;
            }
        }
        class OperationDiv : operation
        {
            public override double GetResult()
            {
                double result = 0;
                if (NumberB != 0)
                {
                    result = NumberA / NumberB;
                    return result;
                }
                else
                {
                    throw new Exception("除数不能为0。");
                }
            }
        }
        //简单运算工厂类
        public class OperationFactory
        {
            public static operation  createOperation(string operate)
            {
                operation oper = null;
                switch (operate)
                {
                    case "+":
                        oper=new OperationAdd();
                        break;
                    case "-":
                        oper=new OperationSub();
                        break;
                    case "*":
                        oper=new OperationMul();
                        break;
                    case "/":
                        oper=new OperationDiv();
                        break;
                }
                return oper;
            }

        }


        class Program
        {
            static void Main(string[] args)
            {
                operation oper; 
                oper = OperationFactory.createOperation("/");
                oper.NumberA = 23;
                oper.NumberB = 1;
                double result = oper.GetResult();
            }
        }
    }

  • 相关阅读:
    不写代码能实现APP消息推送吗
    如何让智能设备接入天猫精灵,实现语音控制功能
    给GoKit3(STM32)装一块N102,在家就能体验NB-IoT开发啦
    ESP8266 NodeMcu机智云SOC方案开发经验分享
    利用map和reduce编写一个str2float函数
    代码学习(1)
    箱线图boxplot()的绘制
    mysql远程访问数据库的问题解决
    codeforces 596 C. p-binary
    主席树的妙用——Just h-index
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9664014.html
Copyright © 2020-2023  润新知