Chain of responsibility : Chain of Responsibility Patterns « Design Pattern « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Design Pattern » Chain of Responsibility Patterns 
34.7.2.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.12.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.34.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.56.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.56.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.02.01.02.0, -1.03.04.05.04.0 });
    System.out.println(((DocumentDocumentFormat.solve(line)).data);
  }
}
34.7.Chain of Responsibility Patterns
34.7.1.Watch What is Going On with the Chain of Responsibility Patterns
34.7.2.Chain of responsibility
34.7.3.Multiple dispatching
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.