使用简单工厂模式构造四则计算器

前言

工厂模式就是专门负责将大量有共同接口的类实例化,而且不必事先知道每次是要实例化哪一个类的模式。它定义一个用于创建对象的接口,由子类决定实例化哪一个类。
这篇文章还在四则计算器的基础上添加了指数对数的运算。(类名是因为实验报告的要求,如影响观感还请见谅)

设计计算器

两个数的加减乘除、指数和对数的运算方法

//加法
public class Oyy22AddCalculator implements Oyy22Calculator{
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        result = numberA + numberB;
        return result;
    }
}
//减法
public class Oyy22SubCalculator implements Oyy22Calculator {
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        result = numberA - numberB;
        return result;
    }
}
//乘法
public class Oyy22MulCalculator implements Oyy22Calculator {
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        result = numberA * numberB;
        return result;
    }
}
//除法
public class Oyy22DivCalculator implements Oyy22Calculator {
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        if (numberB==0) {
            throw new IllegalArgumentException("除数不能为零");
        }else {
            result = numberA / numberB;
        }
        return result;
    }
}
//指数运算
public class Oyy22PowCalculator implements Oyy22Calculator {
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        result = Math.pow(numberA,numberB);
        return result;
    }
}
//对数运算
public class Oyy22LogCalculator implements Oyy22Calculator {
    @Override
    public double getResult(double numberA, double numberB) {
        double result = 0;
        result = Math.log(numberA)/Math.log(numberB);;
        return result;
    }
}

工厂方法

public class Oyy22AddFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22AddCalculator();
    }
}

public class Oyy22SubFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22SubCalculator();
    }
}

public class Oyy22MulFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22MulCalculator();
    }
}

public class Oyy22DivFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22DivCalculator();
    }
}

public class Oyy22PowFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22PowCalculator();
    }
}

public class Oyy22LogFactory implements Oyy22Factory{
    @Override
    public Oyy22Calculator CreateOyy22Calculator() {
        return new Oyy22LogCalculator();
    }
}

工厂方法接口设计

public interface Oyy22Factory {
    public static Oyy22Calculator createOyy22Calculator(String calculator) {
        Oyy22Calculator cal=null;
        switch(calculator) {
            case "+":
                cal = new Oyy22AddCalculator();
                break;
            case "-":
                cal = new Oyy22SubCalculator();
                break;
            case "*":
                cal = new Oyy22MulCalculator();
                break;
            case "/":
                cal = new Oyy22DivCalculator();
                break;
            case "^":
                cal = new Oyy22PowCalculator();
            case "log" :
                cal = new Oyy22LogCalculator();
        }
        return cal;
    }
    Oyy22Calculator CreateOyy22Calculator();
}

计算器接口设计

public interface Oyy22Calculator{
    public double getResult(double numberA,double numberB);
}

测试类

public class Test {
    public static void main(String[] args) {
        Oyy22Calculator calculator = null;
        calculator = Oyy22Factory.createOyy22Calculator("-");
        double numberA = 10;
        double numberB = 3;
        System.out.println(calculator.getResult(numberA, numberB));
    }
}

使用简单工厂模式构造四则计算器_第1张图片
总体来说没什么问题吧,就是指数对数那里输出有点问题。

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