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

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

功能要求:

(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();
        }
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值