大话设计模式-简单工程模式

前言:

简单工厂模式是这23个模式里的第一个模式,也是比较简单和体现非常明确的一个模式。通过简单工厂可以非常好的体验出面向对象编程的好处,面向对象概念的重要性。也算是通过这个模式体验设计模式的好处把!

目录

概念:

需求:

实现:

业务逻辑:

     运算基类:

运算具体的派生类:

运算工厂: 

客户端:

结果:


概念:

根据客户端的选择进行动态的实例化对象,也就是根据用户的需求来实例化具体的对象。

需求:

实现一个有加减乘除的计算器,利用简单工厂模式实现。

实现:

业务逻辑:

     运算基类:

 /// <summary>
    /// 计算类
    /// </summary>
    public class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;
        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }//end NumberA
        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }//end NumberB
        //计算方法--虚方法
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }//end Pperaton
    

运算具体的派生类:

       

/// <summary>
    /// 加法运算,继承运算类。
    /// </summary>
    class OpertionAdd : Operation
    {
        //重新父类的计算方法
        public override double GetResult()
        {
            double result = 0;//计算结果
            result = NumberA + NumberB;//具体运算
            return result;//返回结果
        }//end Getresult
    }//end OpertonAdd
    /// <summary>
    /// 减法
    /// </summary>
    class OpertiononSub : Operation
    {
        //重新父类的计算方法
        public override double GetResult()
        {
            double result = 0;//计算结果
            result = NumberA - NumberB;//具体运算
            return result;//返回结果
        }//end Getresult

    }//end OPertionsub
    /// <summary>
    /// 乘法
    /// </summary>
    class OpertionMul : Operation
    {
        //重新父类的计算方法
        public override double GetResult()
        {
            double result = 0;//计算结果
            result = NumberA * NumberB;//具体运算
            return result;//返回结果
        }//end Getresult
    }//end OpertionMul
    /// <summary>
    /// 除法
    /// </summary>
    class OpertionDiv : Operation
    {
        //重新父类的计算方法
        public override double GetResult()
        {
            double result = 0;//计算结果
            if (NumberB == 0)
                throw new Exception("除数不能为0.");

            result = NumberA / NumberB;//具体运算
            return result;//返回结果
        }//end Getresult
    }
    //end OpertionDiv

运算工厂: 

/// <summary>
    /// 简单工厂类,根据需求实例化类。
    /// </summary>
    public class OperationFactory
    {
        /// <summary>
        /// 实例化具体的运算类
        /// </summary>
        /// <param name="operate">运算符</param>
        /// <returns>返回具体实例化的类</returns>
        public static Operation createOperate(string operate)

        {
            Operation oper = null;//实例化父类,从新赋值。
            //判断
            switch (operate)
            {
                case "+":
                    //把父类,转换成子类
                    oper = new OpertionAdd();//实例化加法类
                    break;
                case "-":
                    //把父类,转换成子类
                    oper = new OpertiononSub();//实例化减法类
                    break;
                case "*":
                    //把父类,转换成子类
                    oper = new OpertionMul();//实例化乘法类
                    break;
                case "/":
                    //把父类,转换成子类
                    oper = new OpertionDiv();//实例化除法类
                    break;

            }//end swithc
            //返回结果
            return oper;
        }
    }//end OperationFactory

客户端:

//确定按钮
private void button1_Click(object sender, EventArgs e)
        {   //运算类
            Operation ope;
            //从新给父类,赋值。
         
            ope = OperationFactory.createOperate(txt2.Text);   //运算符
            ope.NumberA = int.Parse(txt1.Text);//给A赋值
            ope.NumberB =int.Parse( txt3.Text);//给B赋值
            //调用计算方法,输出结果。
            double result = ope.GetResult();
            txt4.Text = result.ToString();

        }

 

结果:

posted @ 2019-11-06 14:52  康世行  阅读(25)  评论(0编辑  收藏  举报