Improved documentation and implementation notes for Function

generic-observer
Selebrator 7 years ago
parent 0760b65a8b
commit b8da8cd419

@ -2,22 +2,47 @@ package _4._1;
public abstract class Function {
protected Function predecessor;
/**
* For functions f and g,
* g is the inner function of f in f(g(x))
*/
protected Function inner;
public Function() {
this.predecessor = null;
this.inner = null;
}
public Function(Function predecessor) {
this.predecessor = predecessor;
public Function(Function inner) {
this.inner = inner;
}
/**
* <b>Note</b>: This method should probably not be overridden.
* <p>
* Computes the value of this (nested) function for x.
* <p>
* If this function is f and it's inner function is g,
* then compute(x) returns f(g(x))
*
* @see #apply(double)
*/
public double calculate(double x) {
if(this.predecessor == null) {
if(this.inner == null) {
return this.apply(x);
}
return this.predecessor.calculate(x);
return this.inner.calculate(x);
}
/**
* Computes the value of this function for x
* <p>
* If this function is f and it's inner function is g,
* then apply(x) returns f(x)
*/
/*
* protected to allow the this method to manipulate state
* and still allow the developer to make assumptions about the state.
* In most cases the implementation can be public.
*/
protected abstract double apply(double x);
}

@ -5,8 +5,8 @@ public class SineFunction extends Function {
super();
}
public SineFunction(Function predecessor) {
super(predecessor);
public SineFunction(Function inner) {
super(inner);
}
@Override

@ -5,12 +5,12 @@ public class SquareFunction extends Function {
super();
}
public SquareFunction(Function predecessor) {
super(predecessor);
public SquareFunction(Function inner) {
super(inner);
}
@Override
protected double apply(double x) {
public double apply(double x) {
return x * x;
}
}

Loading…
Cancel
Save