2015-09-07 11:44:36 -06:00
|
|
|
// patterns/chain/ChainOfResponsibility.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java patterns.chain.ChainOfResponsibility}
|
2015-06-15 17:47:35 -07:00
|
|
|
package patterns.chain;
|
|
|
|
import java.util.*;
|
2015-11-03 12:00:44 -08:00
|
|
|
import java.util.function.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
interface Algorithm {
|
2015-11-03 12:00:44 -08:00
|
|
|
Result algorithm(List<Double> line);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:00:44 -08:00
|
|
|
class FindMinima {
|
2021-03-04 16:15:04 -07:00
|
|
|
public static Result test(
|
|
|
|
boolean success, String id, double d1, double d2) {
|
|
|
|
System.out.println(id);
|
|
|
|
if(success) // Actual test/calculation here
|
|
|
|
return new Result(Arrays.asList(d1, d2));
|
2015-06-15 17:47:35 -07:00
|
|
|
else // Try the next one in the chain:
|
2021-03-04 16:15:04 -07:00
|
|
|
return Result.fail;
|
|
|
|
}
|
|
|
|
public static Result leastSquares(List<Double> line) {
|
|
|
|
return test(false, "LeastSquares", 1.1, 2.2);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
public static Result perturbation(List<Double> line) {
|
2021-03-04 16:15:04 -07:00
|
|
|
return test(false, "Perturbation", 3.3, 4.4);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
public static Result bisection(List<Double> line) {
|
2021-03-04 16:15:04 -07:00
|
|
|
return test(true, "Bisection", 5.5, 6.6);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2017-05-10 11:45:39 -06:00
|
|
|
static List<Function<List<Double>, Result>>
|
|
|
|
algorithms = Arrays.asList(
|
2015-11-03 12:00:44 -08:00
|
|
|
FindMinima::leastSquares,
|
|
|
|
FindMinima::perturbation,
|
|
|
|
FindMinima::bisection
|
|
|
|
);
|
|
|
|
public static Result minima(List<Double> line) {
|
2017-05-10 11:45:39 -06:00
|
|
|
for(Function<List<Double>, Result> alg :
|
|
|
|
algorithms) {
|
2015-11-03 12:00:44 -08:00
|
|
|
Result result = alg.apply(line);
|
2015-06-15 17:47:35 -07:00
|
|
|
if(result.success)
|
|
|
|
return result;
|
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
return Result.fail;
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ChainOfResponsibility {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void main(String[] args) {
|
2015-06-15 17:47:35 -07:00
|
|
|
FindMinima solver = new FindMinima();
|
2015-11-03 12:00:44 -08:00
|
|
|
List<Double> line = Arrays.asList(
|
2015-06-15 17:47:35 -07:00
|
|
|
1.0, 2.0, 1.0, 2.0, -1.0,
|
2015-11-03 12:00:44 -08:00
|
|
|
3.0, 4.0, 5.0, 4.0);
|
2015-06-15 17:47:35 -07:00
|
|
|
Result result = solver.minima(line);
|
|
|
|
if(result.success)
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(result.line);
|
2015-06-15 17:47:35 -07:00
|
|
|
else
|
|
|
|
System.out.println("No algorithm found");
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2021-03-04 16:15:04 -07:00
|
|
|
LeastSquares
|
|
|
|
Perturbation
|
|
|
|
Bisection
|
2015-11-03 12:00:44 -08:00
|
|
|
[5.5, 6.6]
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|