zoukankan      html  css  js  c++  java
  • 大话设计模式之简单工厂模式代码

    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();
            }
        }
    }

  • 相关阅读:
    PostgreSQL备份工具-pg_probackup
    Multi-Master Replication Solutions for PostgreSQL
    PostgreSQL高可用:多主复制解决方案
    Postgressql高可用(pgpool+异步流复制)转
    AudioFlinger
    GPIO口配置为上拉,下拉输入
    转【Qualcomm高通音频】音效调试_录音文件播放有杂音,如何定位原因?
    转【Qualcomm高通音频】如何使用QXDM、QCAT、CoolEditor音频日志抓取、解析和分析?
    转【Qualcomm高通音频】调试工具QACT_如何新增一套音效
    转【Qualcomm高通音频】调试工具QACT_如何更换音效的音频拓扑
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9664014.html
Copyright © 2011-2022 走看看