Java Tutorial/Design Pattern/Chain of Responsibility Patterns
Версия от 17:44, 31 мая 2010; (обсуждение)
Chain of responsibility
// Carries the result data and whether the strategy was successful:
class Document {
public double[] data;
public Document(double[] data) {
this.data = data;
}
private boolean succeeded;
public boolean isSuccessful() {
return succeeded;
}
public void setSuccessful(boolean b) {
succeeded = b;
}
}
interface Format {
Document doFormat(Document m);
}
class EnlargeFont implements Format {
public Document doFormat(Document m) {
System.out.println("Trying EnlargeFont algorithm");
Document r = new Document(new double[] { 1.1, 2.2 }); // Dummy data
r.setSuccessful(false);
return r;
}
}
class ChangeColor implements Format {
public Document doFormat(Document m) {
System.out.println("Trying ChangeColor algorithm");
Document r = new Document(new double[] { 3.3, 4.4 }); // Dummy data
r.setSuccessful(false);
return r;
}
}
class AddHeader implements Format {
public Document doFormat(Document m) {
System.out.println("Trying AddHeader algorithm");
Document r = new Document(new double[] { 5.5, 6.6 }); // Dummy data
r.setSuccessful(true);
return r;
}
}
class AddFooter implements Format {
public Document doFormat(Document m) {
System.out.println("Trying AddFooter algorithm");
Document r = new Document(new double[] { 5.5, 6.6 }); // Dummy data
r.setSuccessful(true);
return r;
}
}
class DocumentFormat {
private static Format[] solutions = { new EnlargeFont(), new ChangeColor(), new AddHeader(),
new AddFooter(), };
public static Document solve(Document line) {
Document r = line;
for (int i = 0; i < solutions.length; i++) {
r = solutions[i].doFormat(r);
if (r.isSuccessful())
return r;
}
throw new RuntimeException("unsolved: " + line);
}
}
public class ChainOfResposibilityDemo {
public static void main(String args[]) {
Document line = new Document(new double[] { 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0 });
System.out.println(((Document) DocumentFormat.solve(line)).data);
}
}
Multiple dispatching
import java.util.Random;
class Result {
private String name;
private Result(String name) {
this.name = name;
}
public final static Result LessThan = new Result("Less than"), Equal = new Result("equal"),
GreaterThan = new Result("Greater than");
public String toString() {
return name;
}
}
interface Number {
Result compete(Number it);
Result eval(One p);
Result eval(Two s);
Result eval(Three r);
}
class One implements Number {
public Result compete(Number it) {
return it.eval(this);
}
public Result eval(One p) {
return Result.GreaterThan;
}
public Result eval(Two s) {
return Result.LessThan;
}
public Result eval(Three r) {
return Result.Equal;
}
public String toString() {
return "One";
}
}
class Two implements Number {
public Result compete(Number it) {
return it.eval(this);
}
public Result eval(One p) {
return Result.Equal;
}
public Result eval(Two s) {
return Result.GreaterThan;
}
public Result eval(Three r) {
return Result.LessThan;
}
public String toString() {
return "Two";
}
}
class Three implements Number {
public Result compete(Number it) {
return it.eval(this);
}
public Result eval(One p) {
return Result.LessThan;
}
public Result eval(Two s) {
return Result.Equal;
}
public Result eval(Three r) {
return Result.GreaterThan;
}
public String toString() {
return "Three";
}
}
class NumberGenerator {
private static Random rand = new Random();
public static Number newItem() {
switch (rand.nextInt(3)) {
default:
case 0:
return new Two();
case 1:
return new One();
case 2:
return new Three();
}
}
}
class Compete {
public static void match(Number a, Number b) {
System.out.println(a + " " + a.rupete(b) + " vs. " + b);
}
}
public class MultipleDispathDemo {
public static void main(String args[]) {
int SIZE = 20;
for (int i = 0; i < SIZE; i++)
Compete.match(NumberGenerator.newItem(), NumberGenerator.newItem());
}
}
Watch What is Going On with the Chain of Responsibility Patterns
public class MainClass {
public static void main(String args[]) {
Application app = new Application();
IntermediateLayer intermediateLayer = new IntermediateLayer(app);
FrontEnd frontEnd = new FrontEnd(intermediateLayer);
frontEnd.getHelp(HelpType.GENERAL_HELP);
}
}
class HelpType {
public final static int FRONT_END_HELP = 1;
public final static int INTERMEDIATE_LAYER_HELP = 2;
public final static int GENERAL_HELP = 3;
}
interface HelpInterface {
public void getHelp(int helpConstant);
}
class IntermediateLayer implements HelpInterface {
HelpInterface successor;
public IntermediateLayer(HelpInterface s) {
successor = s;
}
public void getHelp(int helpConstant) {
if (helpConstant != HelpType.INTERMEDIATE_LAYER_HELP) {
successor.getHelp(helpConstant);
} else {
System.out.println("intermediate");
}
}
}
class FrontEnd implements HelpInterface {
HelpInterface successor;
public FrontEnd(HelpInterface s) {
successor = s;
}
public void getHelp(int helpConstant) {
if (helpConstant != HelpType.FRONT_END_HELP) {
successor.getHelp(helpConstant);
} else {
System.out.println("front end");
}
}
}
class Application implements HelpInterface {
public void getHelp(int helpConstant) {
System.out.println("application");
}
}