简单工程模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。 


使用简单工程模式实现一个计算器类:



源代码如下:

// Operation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    public class Operation// 此类为父类
    {
        private double _numberA = 0;  /// 数字A 
        private double _numberB = 0;  /// 数字B 
        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;
        }
    }
}

// ChildOperation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class OperationAdd:Operation
    {
        public override double  GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            //return result ;
            return base.GetResult();
        }
    }
    
    class OperationSub:Operation
    {
        public override double  GetResult()
        {
 	         double result = 0;
           result = NumberA - NumberB;
           //return result;
           return base.GetResult();
        }
    }
    class OperationMul:Operation
    {
        public override double  GetResult()
       {
 	       double result = 0;
           result = NumberA * NumberB;
           return result;
           //return base.GetResult();
        }
    }
    class OperationDiv:Operation
    {
        public override double  GetResult()
        {
            double result = 0;
            result = NumberA / NumberB;
            return result;
            //return base.GetResult();
        }
    }
}

/// OperationFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class OperationFactory
    {
        public static Operation createOperate(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;
        }
    }
}


///Program.cs 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
           try
           {
               Console.Write("请输入数字A:");
               string strNumberA = Console.ReadLine();
               Console.Write("请选择运算符号(+,-,*,/):");
               string strOperation = Console.ReadLine();
               Console.Write("请输入数字B:");
               string strNumberB = Console.ReadLine();
               //string strResult = "";
              // strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperation));
               //    Console.WriteLine("结果是:"+strResult);

               Operation oper;// 
               oper = OperationFactory.createOperate(strOperation);
               oper.NumberA = Convert.ToDouble(strNumberA);
               oper.NumberB = Convert.ToDouble(strNumberB);
               double result = oper.GetResult();
               Console.Write(result);

           }
           catch (System.Exception ex)
           {
           	    Console.WriteLine("输入有误:"+ex.Message);
           }
        }
    }
}


参考资料: 《大话设计模式》

UML工具: StarUML

代码调试工具:VS2008 



Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐