Java 数学函数 实现加减乘除 代理模式 && 装饰模式

完整代码

​interface math {

    double result(double x);//计算带入具体x的返回结果
    math set(double x);//不同计算增加的内容


}

public class project7 {
    public static void main(String[] args) {
        // x  当 x = 3
        System.out.println(new Function().result(3));
        // x+x  当 x = 4
        System.out.println(new Function().addition().result(4));
        // (x+x)*3  当 x = 5
        System.out.println(new Function().addition().multiplication(3).result(5));

    }
}

//函数
class Function {
    private math function;
    double x;

    public double result(double x) {//带入值计算返回结果
        this.x = x;
        return function.result(x);
    }
    //创建函数
    public Function() {
        this.function = new Subject();
    }

    //传参的位常数 不传参的是自变量

    //加法
    public Function addition(double x) {
        function = new addition(function).set(x);
        return this;
    }
    public Function addition() {
        function = new addition(function).Add();
        return this;
    }

    //乘法
    public Function multiplication(double x) {
        function = new multiplication(function).set(x);
        return this;
    }
    public Function multiplication() {
        function = new multiplication(function).Add();
        return this;
    }

    //减法
    public Function subtraction (double x) {
        function = new subtraction(function).set(x);
        return this;
    }
    public Function subtraction () {
        function = new subtraction(function).Add();
        return this;
    }

    //除法
    public Function division(double x) {
        function = new division(function).set(x);
        return this;
    }
    public Function division() {
        function = new division(function).Add();
        return this;
    }


}

class Subject implements math {
    math target,X = null;
    double x;

    public Subject() { }

    public double result(double x) {
        return x;
    }

    public Subject(math target) {
        this.target = target;
    }

    public math set(double x) {
        this.x = x;
        return this;
    }


    private double R(double x) {
        return X.result(x);
    }

    //增加新变量
    public math Add(){
        X = new Subject();
        return this;
    }

}

class addition extends Subject {

    public addition(math target) {
        super(target);
    }

    public double result(double x) {
        return target.result(x) + ( X == null ? this.x : X.result(x) );
    }



}

class subtraction extends Subject {

    public subtraction(math target) {
        super(target);
    }

    public double result(double x) {
        return target.result(x) - ( X == null ? this.x : X.result(x) );
    }
}

class multiplication extends Subject {

    public multiplication(math target) {
        super(target);
    }

    public double result(double x) {
        return target.result(x) * ( X == null ? this.x : X.result(x) );
    }
}

class division extends Subject {

    public division(math target) {
        super(target);
    }

    public double result(double x) {
        return target.result(x) / ( X == null ? this.x : X.result(x) );
    }
}

解释

math 抽象组件

在这里插入图片描述

Subject 抽象装饰类 (真实角色)

成员变量target是一定存在的,X是后添加的函数可有可无

在这里插入图片描述

具体装饰类

加减乘除为具体的装饰类
X == null ? this.x : X.result(x)

这个意思是判断有没有增加自变量,如果add的是自变量就让参数和X关联
如果添加的不是自变量add的就是常数,跟参数无关

在这里插入图片描述

Function 代理角色

具体函数
有点小毛病就是不能实现类似于

6x3x+2 \frac{6x}{3x+2}

上下都是函数的

需要建立两个分子代理和分母代理才能间接实现

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章