OnJava8-Examples/patterns/chain/ChainOfResponsibility.java

91 lines
2.4 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/chain/ChainOfResponsibility.java
2020-10-07 13:35:40 -06:00
// (c)2020 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-01-25 18:05:55 -08:00
// Using the Functional interface
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
class Result {
boolean success;
2015-11-03 12:00:44 -08:00
List<Double> line;
2017-05-01 14:33:10 -06:00
Result(List<Double> data) {
2015-06-15 17:47:35 -07:00
success = true;
line = data;
}
2017-05-01 14:33:10 -06:00
Result() {
2015-06-15 17:47:35 -07:00
success = false;
2015-11-03 12:00:44 -08:00
line = Collections.<Double>emptyList();
2015-06-15 17:47:35 -07:00
}
}
class Fail extends Result {}
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 {
public static Result leastSquares(List<Double> line) {
2015-06-15 17:47:35 -07:00
System.out.println("LeastSquares.algorithm");
boolean weSucceed = false;
if(weSucceed) // Actual test/calculation here
2015-11-03 12:00:44 -08:00
return new Result(Arrays.asList(1.1, 2.2));
2015-06-15 17:47:35 -07:00
else // Try the next one in the chain:
return new Fail();
}
2015-11-03 12:00:44 -08:00
public static Result perturbation(List<Double> line) {
2015-06-15 17:47:35 -07:00
System.out.println("Perturbation.algorithm");
boolean weSucceed = false;
if(weSucceed) // Actual test/calculation here
2015-11-03 12:00:44 -08:00
return new Result(Arrays.asList(3.3, 4.4));
2015-06-15 17:47:35 -07:00
else
return new Fail();
}
2015-11-03 12:00:44 -08:00
public static Result bisection(List<Double> line) {
2015-06-15 17:47:35 -07:00
System.out.println("Bisection.algorithm");
boolean weSucceed = true;
if(weSucceed) // Actual test/calculation here
2015-11-03 12:00:44 -08:00
return new Result(Arrays.asList(5.5, 6.6));
2015-06-15 17:47:35 -07:00
else
return new Fail();
}
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) {
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;
}
return new Fail();
}
}
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:
2015-06-15 17:47:35 -07:00
LeastSquares.algorithm
Perturbation.algorithm
Bisection.algorithm
2015-11-03 12:00:44 -08:00
[5.5, 6.6]
2015-09-07 11:44:36 -06:00
*/