感谢您阅读我的博客,如果您现在工作、学习累了或者疲惫了,不妨聆听一下音乐,它能够减轻你的疲劳,还能够带给您一种舒适愉悦的心情。如果您认为这篇文章还不错或者有所收获,您可以在页面 右侧和底部 扫描二维码 打赏我,您的鼓励是我继续写作、分享的最大动力!

.NET设计模式之简单工厂模式

1.代码结构

2.Operation.cs
using System;

namespace CalculatorDemoWithMindOfObject
{
    public class Operation
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }

        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }

    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }

    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }

    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }

    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0)
            {
                throw new Exception("除数不能为0");
            }
            double result = 0;
            result = NumberA / NumberB;
            return result;
        }
    }
}
3.OperationFactory.cs
namespace CalculatorDemoWithMindOfObject
{
    /// <summary>
    ///     运算类工厂(简单工厂模式)
    /// </summary>
    public class OperationFactory
    {
        public static Operation CreateOperation(string operatorInfo)
        {
            Operation operation = null;
            switch (operatorInfo)
            {
                case "+":
                    operation = new OperationAdd();
                    break;

                case "-":
                    operation = new OperationSub();
                    break;

                case "*":
                    operation = new OperationMul();
                    break;

                case "/":
                    operation = new OperationDiv();
                    break;
            }
            return operation;
        }
    }
}
4.Program.cs
using System;

namespace CalculatorDemoWithMindOfObject
{
    /// <summary>
    ///     使用面向对象三大特征(封装、继承、多态)实现的计算器
    ///     LDH @ 2019-2-12
    /// </summary>
    class Program
    {
        static void Main()
        {
            Console.Title = "使用面向对象三大特征,实现简单的计算器功能";

            Operation op = OperationFactory.CreateOperation("+");
            op.NumberA = 10;
            op.NumberB = 20;

            double result = op.GetResult();
            Console.WriteLine("运算结果:" + result);

            Console.ReadKey();
        }
    }
}
5.运行结果

posted @ 2019-02-13 09:22  Love In Winter  阅读(606)  评论(0)    收藏  举报
作者: LifeDecidesHappiness
出处: http://www.cnblogs.com/LifeDecidesHappiness/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,否则保留追究法律责任的权利,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址 2468881301@qq.com  联系我,非常感谢。
踏实做一个为人民服务的搬运工!
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能,您的支持和鼓励是我继续写作、分享的最大动力!

点击关注不迷路,让我带你上高速!