简单工厂模式

利用C#语言编写一个计算器程序

面向过程写法(一般写法):

//主函数代码
Console.WriteLine("请输入第一个数字:");
string strNum1 = Console.ReadLine();
Console.WriteLine("请输入第二个数字:");
string strNum2 = Console.ReadLine();
Console.WriteLine("请输入运算符:");
string strOperation = Console.ReadLine();
string result = null;
switch (strOperation)
{
    case "+":
        result = Convert.ToString(Convert.ToDouble(strNum1) + Convert.ToDouble(strNum2));
        break;
    case "-":
        result = Convert.ToString(Convert.ToDouble(strNum1) - Convert.ToDouble(strNum2));
        break;
    case "*":
        result = Convert.ToString(Convert.ToDouble(strNum1) * Convert.ToDouble(strNum2));
        break;
    case "/":
        if (strNum2 == "0") Console.WriteLine("除数不能为零");
        else result = Convert.ToString(Convert.ToDouble(strNum1) / Convert.ToDouble(strNum2));
        break;
}
Console.WriteLine("结果是:" + result);
Console.ReadKey();

缺点:业务逻辑与界面逻辑挤在一起,耦合度高。

第一次修改:将操作封装成类,把界面与操作分开,降低耦合度。

//操作类
class Opaeration
{
    public static double GetResult(double num1, double num2, string operate)
    {
        double result = 0;
        switch (operate)
        {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                if (num2 == 0) Console.WriteLine("除数不能为零");
                else result = num1 / num2;
                break;
        }
        return result;
    }
}
//主函数
 static void Main(string[] args)
 {
     try
     {
         Console.WriteLine("请输入第一个数字:");
         string strNum1 = Console.ReadLine();
         Console.WriteLine("请输入第二个数字:");
         string strNum2 = Console.ReadLine();
         Console.WriteLine("请输入运算符:");
         string strOperation = Console.ReadLine();
         double result = 0;
         result = Opaeration.GetResult(Convert.ToDouble(strNum1), Convert.ToDouble(strNum2), strOperation);
         Console.WriteLine("结果是:" + result);
         Console.ReadKey();
     }
     catch (Exception ex)
     {
         Console.WriteLine("输入有误:" + ex.Message);
         Console.ReadKey();
     }
 }

缺点:当添加一个新的运算,需要让加减乘除的运算都参与编译;运算符之间的耦合度还是高。

第二次修改:进一步将各个运算封装成类,降低耦合

//运算操作的父类
class Operation
{
    private double _num1 = 0;
    private double _num2 = 0;
    public double Num1 { get => _num1; set => _num1 = value; }
    public double Num2 { get => _num2; set => _num2 = value; }

    public virtual double GetResult()   //利用虚方法实现多态
    {
        double result = 0;
        return result;
    }
}
//各个运算类
class OperationAdd : Operation
{
    public override double GetResult()
    {
        double result = 0;
        result = Num1 + Num2;
        return result;
    }
}
class OperationSub : Operation
{
    public override double GetResult()
    {
        double result = 0;
        result = Num1 - Num2;
        return result;
    }
}
class OperationMul : Operation
{
    public override double GetResult()
    {
        double result = 0;
        result = Num1 * Num2;
        return result;
    }
}
class OperationDiv : Operation
{
    public override double GetResult()
    {
        double result = 0;
        if (Num2 == 0) throw new Exception("除数不能为零");
        result = Num1 / Num2;
        return result;
    }
}

接下来是考虑如何实例化对象的问题:用一个单独的类来做创造实例的过程,这个类就是工厂。

简单工厂类:

public class OperationFactory
{
    public static Operation createOperate(string operate)
    {
        Operation op = null;
        switch (operate)
        {
            case "+":
                op = new OperationAdd();
                break;
            case "-":
                op = new OperationSub();
                break;
            case "*":
                op = new OperationMul();
                break;
            case "/":
                op = new OperationDiv();
                break;
        }
        return op;
    }
}

这个时候,需要修改已有的运算时仅需要改对应的类就行了;需要添加运算时,则添加对应类和添加工厂里switch分支。
客户端代码:

Operation op;
op = OperationFactory.createOperate("/");
op.Num1 = 10;
op.Num2 = 11;
double result;
result =  op.GetResult();
Console.WriteLine(result);

Console.ReadKey();

你可能感兴趣的:(简单工厂模式)