I want to make a class which applies Activation Function
to a value. The class is as follows
public class ActivationFunction {
public static double function(double value, Functions functions) {
if(functions.equals(Functions.SIGMOID)) {
return sigmoid(value);
}
return 0f;
}
public static double derivativeOfFunction(double value, Functions functions) {
if(functions.equals(Functions.SIGMOID))
return sigmoidDerivative(value);
return 0f;
}
private static double sigmoid(double value) {
return 1 / (1 + Math.exp(0 - value));
}
private static double sigmoidDerivative(double value) {
return ( Math.exp(0 - value) / ( (1 + Math.exp(0 - value)) * (1 + Math.exp(0 - value)) ) );
}
}
Where Functions
is the enum
in which different functions are defined. There is only sigmoid
function now, but more will be added.
QUESTION
I thing it's violating the Open-Closed Principle
, one of the 5 SOLID
Principles of OOP (may be it's violating more). So, what is the correct way to write this class to accommodate addition of more functions in future?
Any help is appreciated.
You could place the implementation of the common interface in the enum itself.
Something like this:
public enum Functions {
SIGMOID {
public double function(double value) { return 1 / (1 + Math.exp(0 - value)); }
public double derivative(double value) { return ...; }
},
OTHER_FUNCTIONS { ... }
public abstract double function(double value);
public abstract double derivative(double value);
}
Your ActivationFunction
then becomes very simple to write - it's probably not even useful any more.