欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式:(一)简单工厂

程序员文章站 2024-01-21 20:42:16
...

问题:写一个计算器的控制台应用程序

功能要求:

(1)实现+、-、*、/四种基本运算;

(2)程序运行时,用户输入两个数和运算符号,即可得到运算结果。

public class Operation //运算基类
{

    private double _numberA = 0.0;
    private double _numberB = 0.0;

    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.0;
        return result;
    }
}

class OperationAdd : Operation //加法类,继承Operation
{
    public override double GetResult() //重写虚方法
    {
        double result = 0.0;
        result = NumberA + NumberB;
        return result;
    }
}

class OperationSub : Operation //减法类,继承Operation
{
    public override double GetResult() //重写虚方法
    {
        double result = 0.0;
        result = NumberA - NumberB;
        return result;
    }
}

class OperationMul : Operation //乘法类,继承Operation
{
    public override double GetResult() //重写虚方法
    {
        double result = 0.0;
        result = NumberA * NumberB;
        return result;
    }
}

class OperationDiv : Operation //除法类,继承Operation
{
    public override double GetResult() //重写虚方法
    {
        double result = 0.0;
        if (NumberB == 0)
            throw new Exception("除数不能为0。");
        result = NumberA / NumberB;
        return result;
    }
}

public 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;
    }
}
class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.Write("请输入数字A: ");
            string strNumberA = Console.ReadLine(); //接受屏幕输入的数值1

            Console.Write("请选择运算符号(+、-、*、/): ");
            string strOperate = Console.ReadLine(); //用户选择运算符号

            Console.Write("请输入数字B: ");
            string strNumberB = Console.ReadLine(); //接受屏幕输入的数值2

            double strResult = 0.0;

            Operation oper; //声明基类型变量

            oper = OperationFactory.createOperate(strOperate); //根据用户的输入创建子运算类
            oper.NumberA = Convert.ToDouble(strNumberA);
            oper.NumberB = Convert.ToDouble(strNumberB);

            strResult = oper.GetResult(); //多态,调用子类的方法

            Console.WriteLine("结果是 " + strResult);
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine("输入有错", ex.Message);
            Console.ReadLine();
        }
    }
}

参考书籍:大话设计模式-程杰