首页 > Windows开发 > 详细

C#中的简单工厂设计模式示例

时间:2017-10-20 23:58:27      阅读:373      评论:0      收藏:0      [点我收藏+]

这个是用面向对象的方法来实现加,减,乘,除的计算,使用了“简单工厂的设计模式”。


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

namespace 简单公司实现计算1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个数字:");
          double n1=  Convert.ToDouble( Console.ReadLine());
          Console.WriteLine("请输入第二个数字:");
         double n2= Convert.ToDouble(Console.ReadLine());
         Console.WriteLine("请输入一个操作符");
        string oper= Console.ReadLine();
        CalFather cal = Result(oper, n1, n2);
       double result= cal.GetResult();
       Console.WriteLine(result);
       Console.ReadKey();


        }

        /// <summary>
        /// 简单工厂模式
        /// </summary>
        /// <param name="oper">传入的操作符</param>
        /// <param name="n1">第一个运算的数字</param>
        /// <param name="n2">第二个运算的数字</param>
        /// <returns></returns>
        public static CalFather Result(string oper,double n1,double n2)
        {
            CalFather cal = null;
            switch (oper)
            {
                case "+": cal = new Add(n1, n2);
                    break;
                case "-": cal = new Sub(n1, n2);
                    break;
                case "*": cal = new Ride(n1, n2);
                    break;
                case "/":cal=new Chu(n1,n2);
                    break;
                default: Console.WriteLine("输入有误");
                    break;
            }

            return cal;
        }
    }
    /// <summary>
    /// 父类模型,用abstract抽象函数来实现多态
    /// </summary>
    public abstract class CalFather
    {
        public double NumberOne
        {
            get;
            set;
        }
        public double NumberTwo
        {
            get;
            set;
        }

        public CalFather(double One,double Two)
        {
            this.NumberOne = One;
            this.NumberTwo = Two;
        }

        public abstract double GetResult();
    }
    /// <summary>
    /// 加法的子类
    /// </summary>
    public class Add:CalFather
    {
        public Add(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne + this.NumberTwo;
        }
    }
    /// <summary>
    /// 减法的子类
    /// </summary>
    public class Sub:CalFather
    {
        public Sub(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne - this.NumberTwo;
        }
    }
    /// <summary>
    /// 乘法的子类
    /// </summary>
    public class Ride:CalFather
    {
        public Ride(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne * this.NumberTwo;
        }
    }
    /// <summary>
    /// 除法的子类
    /// </summary>
    public class Chu:CalFather
    {
        public Chu(double one,double two):base(one,two)
        {

        }
        public override double GetResult()
        {
            return this.NumberOne / this.NumberTwo;
        }
    }
}


本文出自 “cary_qin的博客” 博客,请务必保留此出处http://xpqinqun.blog.51cto.com/2136/1974499

C#中的简单工厂设计模式示例

原文:http://xpqinqun.blog.51cto.com/2136/1974499

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!