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

  • 相关阅读:
    利用正则表达式限制网页表单里的文本框输入内容小结
    实现注册页面中的倒计时功能代码
    asp.net中Response.Write用法小结
    数据库连接字符串
    asp.net中页面延时跳转代码
    C#网络编程socket使用总结
    CSS选择器总结
    C#面向对象三大特性总结
    HTML总结
    ASP.NET页面生命周期
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9664014.html
Copyright © 2011-2022 走看看