Java设计模式之简单工厂模式

本文介绍了一个简易计算器的设计实现,包括加、减、乘、除及平方根等基本运算,并通过简单工厂模式来创建不同的运算对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//operation.java
import java.math.*;
abstract class operation { //抽象类,抽象出共同属性
    private double numberA = 0;
    private double numberB = 0;

    public double getnumberA(){
        return numberA;
    }
    public void setnumberA(double newA){
        numberA = newA;
    }
    public double getnumberB(){
        return numberB;
    }
    public void setnumberB(double newB){
        numberB = newB;
    }

    abstract public double getResult() throws Exception;
}

class operationAdd extends operation{ //子类重写父类中的抽象方法
     public double getResult(){  //重写抽象类方法,前面不用加override,,中毒太深!!!
        double resu = 0;
        resu = getnumberA() + getnumberB();
        return resu;    
    }
}
class operationSub extends operation{
     public double getResult(){
        double resu = 0;
        resu = getnumberA() - getnumberB();
        return resu;    
    }
}
class operationMul extends operation{
     public double getResult(){
        double resu = 0;
        resu = getnumberA() * getnumberB();
        return resu;    
    }
}
class operationDiv extends operation{
     public double getResult() throws Exception{
        double resu = 0;
        if (getnumberB() == 0){
            throw new Exception("Divider can't be zero...");
        }
        resu = getnumberA() / getnumberB();
        return resu;    
    }
}
class operationSqrt extends operation{
     public double getResult() throws Exception{
        double resu = 0;
        resu = Math.sqrt(getnumberA());
        return resu;    
    }
}

operationFactory.java

import java.util.Scanner;
/*
 * 简单工厂模式:定义一个用于创建对象的接口。
 * 一个工厂类处于对产品类实例化调用的中心位置上,它决定哪一个产品类应当被实例化。
 * 
 */

//如果要增加新的运算,只需在工厂类中添加分支,然后创建相应的运算子类,这样代码易于扩展,易于维护。
public class operationFactory {
    public static operation createOperation(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;
        case "//":   //求一个数的平方根
            op = new operationSqrt();
            break;
        }
        return op;
    }

    public static void main(String[] args) throws Exception{
        System.out.println("Input the operation:");
        Scanner scan = new Scanner(System.in);
        String op = scan.nextLine();
        operation oper = createOperation(op);
        System.out.println("Input the two number:");
        double num1=scan.nextDouble();
        oper.setnumberA(num1);

        double num2=scan.nextDouble();
        oper.setnumberB(num2);

        double resu=oper.getResult();
        System.out.println("result:" + resu);   
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值