carfield.com.hk ChainOfResponsibility.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c06:ChainOfResponsibility.java import com.bruceeckel.util.*; // Arrays2.print() import com.bruceeckel.test.*; import java.util.*; // Carry the information into the strategy: interface Messenger {} // The Result object carries the result data and // whether the strategy was successful: class Result { private boolean succeeded; public boolean isSuccessful() { return succeeded; } public void setSuccessful(boolean b) { succeeded = b; } } abstract class Strategy { abstract public Result strategy(Messenger m); } // Manage the movement through the chain and // find a successful result: class ChainLink { private List chain; private Strategy strat; public ChainLink(List chain, Strategy s) { strat = s; this.chain = chain; chain.add(this); } public ChainLink next() { // Where this link is in the chain: int location = chain.indexOf(this); if (!end()) return (ChainLink)chain.get(location + 1); // Indicates a programming error (thus // doesn't need to be a checked exception): throw new RuntimeException( &quot;Tried to move past end of chain&quot;); } public boolean end() { int location = chain.indexOf(this); return location + 1 &gt;= chain.size(); } public Result strategy(Messenger m) { Result r = strat.strategy(m); if(r.isSuccessful() || end()) return r; return next().strategy(m); } } // For this example, the Messenger // and Result can be the same type: class LineData extends Result implements Messenger { public double[] data; public LineData(double[] data) { this.data = data; } } class LeastSquares extends Strategy { public Result strategy(Messenger m) { System.out.println( &quot;Trying LeastSquares algorithm&quot;); LineData ld = (LineData)m; // [ Actual test/calculation here ] Result r = new LineData( new double[] { 1.1, 2.2 }); // Dummy data r.setSuccessful(false); return r; } } class NewtonsMethod extends Strategy { public Result strategy(Messenger m) { System.out.println( &quot;Trying NewtonsMethod algorithm&quot;); LineData ld = (LineData)m; // [ Actual test/calculation here ] Result r = new LineData( new double[] { 3.3, 4.4 }); // Dummy data r.setSuccessful(false); return r; } } class Bisection extends Strategy { public Result strategy(Messenger m) { System.out.println( &quot;Trying Bisection algorithm&quot;); LineData ld = (LineData)m; // [ Actual test/calculation here ] Result r = new LineData( new double[] { 5.5, 6.6 }); // Dummy data r.setSuccessful(true); return r; } } class ConjugateGradient extends Strategy { public Result strategy(Messenger m) { System.out.println( &quot;Trying ConjugateGradient algorithm&quot;); LineData ld = (LineData)m; // [ Actual test/calculation here ] Result r = new LineData( new double[] { 5.5, 6.6 }); // Dummy data r.setSuccessful(true); return r; } } public class ChainOfResponsibility extends UnitTest { List chain = new ArrayList(); ChainLink[] solutions = { new ChainLink(chain, new LeastSquares()), new ChainLink(chain, new NewtonsMethod()), new ChainLink(chain, new Bisection()), new ChainLink(chain, new ConjugateGradient()), }; LineData line = new LineData(new double[]{ 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0 }); public void test() { Arrays2.print( ((LineData)solutions[0] .strategy(line)).data); } public static void main(String args[]) { new ChainOfResponsibility().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z CommandPattern.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c06:CommandPattern.java import java.util.*; import com.bruceeckel.test.*; interface Command { void execute(); } class Hello implements Command { public void execute() { System.out.print(&quot;Hello &quot;); } } class World implements Command { public void execute() { System.out.print(&quot;World! &quot;); } } class IAm implements Command { public void execute() { System.out.print(&quot;I'm the command pattern!&quot;); } } // An object that holds commands: class Macro { private List commands = new ArrayList(); public void add(Command c) { commands.add(c); } public void run() { Iterator it = commands.iterator(); while(it.hasNext()) ((Command)it.next()).execute(); } } public class CommandPattern extends UnitTest { Macro macro = new Macro(); public void test() { macro.add(new Hello()); macro.add(new World()); macro.add(new IAm()); macro.run(); } public static void main(String args[]) { new CommandPattern().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z StrategyPattern.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c06:strategy:StrategyPattern.java package c06.strategy; import com.bruceeckel.util.*; // Arrays2.print() import com.bruceeckel.test.*; // The strategy interface: interface FindMinima { // Line is a sequence of points: double[] algorithm(double[] line); } // The various strategies: class LeastSquares implements FindMinima { public double[] algorithm(double[] line) { return new double[] { 1.1, 2.2 }; // Dummy } } class NewtonsMethod implements FindMinima { public double[] algorithm(double[] line) { return new double[] { 3.3, 4.4 }; // Dummy } } class Bisection implements FindMinima { public double[] algorithm(double[] line) { return new double[] { 5.5, 6.6 }; // Dummy } } class ConjugateGradient implements FindMinima { public double[] algorithm(double[] line) { return new double[] { 3.3, 4.4 }; // Dummy } } // The &quot;Context&quot; controls the strategy: class MinimaSolver { private FindMinima strategy; public MinimaSolver(FindMinima strat) { strategy = strat; } double[] minima(double[] line) { return strategy.algorithm(line); } void changeAlgorithm(FindMinima newAlgorithm) { strategy = newAlgorithm; } } public class StrategyPattern extends UnitTest { MinimaSolver solver = new MinimaSolver(new LeastSquares()); double[] line = { 1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0 }; public void test() { Arrays2.print(solver.minima(line)); solver.changeAlgorithm(new Bisection()); Arrays2.print(solver.minima(line)); } public static void main(String args[]) { new StrategyPattern().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c06\strategy # using the JDK 1.3 compiler # Invoke with: make HOME := ../../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ StrategyPattern.class jikes: \ StrategyPattern.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif StrategyPattern.class: StrategyPattern.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests c06.strategy.StrategyPattern </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z