zoukankan      html  css  js  c++  java
  • 一 简单工厂模式

    用封装,继承,多态 实现计算器功能
    面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类
    属性类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 属性类
        /// </summary>
       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;
           }
              
        }
    }
    View Code
    加法类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 加法类
        /// </summary>
       public class OperationAdd :Operation
        {
           public override double GetResult()
           {
               double result = 0d;
               result = NumberA + NumberB;
               return result;
           }
        }
    }
    View Code

    除法类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 除法类
        /// </summary>
        public class OperationDivide : Operation
        {
            public override double GetResult()
            {
                double resule = 0d;
                if (NumberB == 0)
                {
                    return resule;
                }
                else
                {
                    resule = NumberA / NumberB;
                }
                return resule;
            }
        }
    }
    View Code

    乘法类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 乘法类
        /// </summary>
        public class OperationMultiply:Operation
        {
            public override double GetResult()
            {
                double result = 0d;
                result = NumberA * NumberB;
                return result;
            }
        }
    }
    View Code

    减法类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 减法类
        /// </summary>
       public class OperationSubtract:Operation
        {
           public override double GetResult()
           {
               double result = 0d;
               result = NumberA - NumberB;
               return result;
           }
        }
    }
    View Code

    简单工厂类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.simpleness
    {
        /// <summary>
        /// 运算类
        /// </summary>
        public class OperationFactory 
        {
            /// <summary>
            /// 运算符
            /// </summary>
            /// <param name="operation"></param>
            /// <returns></returns>
            public static Operation CreateOperation(string operation)
            {
                Operation oper = null;
                switch (operation)
                {
                    case"+":
                        oper = new OperationAdd();
                        break;
                    case"-":
                        oper = new OperationSubtract();
                        break;
                    case"*":
                        oper = new OperationMultiply();
                        break;
                    case"/":
                        oper = new OperationDivide();
                        break;
                }
                return oper;
            }
        }
    }
    View Code

    客户端

     factoryClass.simpleness.Operation oper;
                oper = factoryClass.simpleness.OperationFactory.CreateOperation(this.txtopertion.Text);
                oper.NumberA = Convert.ToDouble(this.txtnumberA.Text);
                oper.NumberB = Convert.ToDouble(this.txtnumberB.Text);
                this.txtresult.Text = oper.GetResult().ToString();
    View Code
  • 相关阅读:
    虚拟主机导入MySQL出现Unknown character set: ‘utf8mb4’
    解决导入MySQL数据库提示"Unknown character set: 'utf8mb4'"错误
    织梦dedecms如何去除版权中的Power by DedeCms
    发送邮件常见出错代码及简单解决方法
    Hadoop集群配置(最全面总结)
    大数据下的数据分析平台架构
    跟上节奏 大数据时代十大必备IT技能
    Hive深入浅出
    深入解析:分布式系统的事务处理经典问题及模型(转载分享)
    数据分析≠Hadoop+NoSQL
  • 原文地址:https://www.cnblogs.com/sl372/p/4544015.html
Copyright © 2011-2022 走看看