Skip to main content
added 134 characters in body
Source Link

Nice improvement is to use Enum with behaviour. This is an example from Effective Java book :

public enum Operation {
    PLUS("+") {
        double apply(double x, double y) {
            return x + y;
        }
    },
    MINUS("-") {
        double apply(double x, double y) {
            return x - y;
        }
    },
    TIMES("*") {
        double apply(double x, double y) {
            return x * y;
        }
    },
    DIVIDE("/") {
        double apply(double x, double y) {
            return x / y;
        }
    };
    private final String symbol;

    Operation(String symbol) {
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return symbol;
    }

    abstract double apply(double x, double y);
}

}

Nice improvement is to use Enum with behaviour. This is an example from Effective Java book :

public enum Operation {
PLUS("+") {
    double apply(double x, double y) {
        return x + y;
    }
},
MINUS("-") {
    double apply(double x, double y) {
        return x - y;
    }
},
TIMES("*") {
    double apply(double x, double y) {
        return x * y;
    }
},
DIVIDE("/") {
    double apply(double x, double y) {
        return x / y;
    }
};
private final String symbol;

Operation(String symbol) {
    this.symbol = symbol;
}

@Override
public String toString() {
    return symbol;
}

abstract double apply(double x, double y);

}

Nice improvement is to use Enum with behaviour. This is an example from Effective Java book :

public enum Operation {
    PLUS("+") {
        double apply(double x, double y) {
            return x + y;
        }
    },
    MINUS("-") {
        double apply(double x, double y) {
            return x - y;
        }
    },
    TIMES("*") {
        double apply(double x, double y) {
            return x * y;
        }
    },
    DIVIDE("/") {
        double apply(double x, double y) {
            return x / y;
        }
    };
    private final String symbol;

    Operation(String symbol) {
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return symbol;
    }

    abstract double apply(double x, double y);
}
Source Link

Nice improvement is to use Enum with behaviour. This is an example from Effective Java book :

public enum Operation {
PLUS("+") {
    double apply(double x, double y) {
        return x + y;
    }
},
MINUS("-") {
    double apply(double x, double y) {
        return x - y;
    }
},
TIMES("*") {
    double apply(double x, double y) {
        return x * y;
    }
},
DIVIDE("/") {
    double apply(double x, double y) {
        return x / y;
    }
};
private final String symbol;

Operation(String symbol) {
    this.symbol = symbol;
}

@Override
public String toString() {
    return symbol;
}

abstract double apply(double x, double y);

}