# HG changeset patch # User Jaroslav Tulach # Date 1251192730 -7200 # Node ID d5b6a877e5a8e6d620d6ab99a88e6719423d6ee0 # Parent 219810ff3c722f44096936c4ae97598a11e914f0 In fact builder pattern is combining mutable objects during creation with immutable result diff -r 219810ff3c72 -r d5b6a877e5a8 samples/aserverinfo/nbproject/build-impl.xml --- a/samples/aserverinfo/nbproject/build-impl.xml Sat Jun 20 16:06:19 2009 +0200 +++ b/samples/aserverinfo/nbproject/build-impl.xml Tue Aug 25 11:32:10 2009 +0200 @@ -20,6 +20,13 @@ --> + + + + + + + - + + + + + + + + + + + + + + + + + @@ -333,10 +368,15 @@ - + + + + + + - + @@ -353,7 +393,7 @@ Must select some files in the IDE or set javac.includes - + @@ -419,11 +459,29 @@ java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + @@ -652,4 +729,20 @@ + + + + + + + + + + + + + + + + diff -r 219810ff3c72 -r d5b6a877e5a8 samples/aserverinfo/nbproject/genfiles.properties --- a/samples/aserverinfo/nbproject/genfiles.properties Sat Jun 20 16:06:19 2009 +0200 +++ b/samples/aserverinfo/nbproject/genfiles.properties Tue Aug 25 11:32:10 2009 +0200 @@ -4,5 +4,5 @@ # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=c82747ea -nbproject/build-impl.xml.script.CRC32=1033418a -nbproject/build-impl.xml.stylesheet.CRC32=84d9cdb5 +nbproject/build-impl.xml.script.CRC32=c7c23170 +nbproject/build-impl.xml.stylesheet.CRC32=29e56e74@1.29.0.45 diff -r 219810ff3c72 -r d5b6a877e5a8 samples/aserverinfo/nbproject/project.properties --- a/samples/aserverinfo/nbproject/project.properties Sat Jun 20 16:06:19 2009 +0200 +++ b/samples/aserverinfo/nbproject/project.properties Tue Aug 25 11:32:10 2009 +0200 @@ -3,6 +3,7 @@ # This directory is removed when the project is cleaned: build.dir=build build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources # Only compile against the classpath explicitly listed here: build.sysclasspath=ignore build.test.classes.dir=${build.dir}/test/classes diff -r 219810ff3c72 -r d5b6a877e5a8 samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java Sat Jun 20 16:06:19 2009 +0200 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java Tue Aug 25 11:32:10 2009 +0200 @@ -6,7 +6,7 @@ import org.apidesign.aserverinfo.spi.URLProvider; /** - * Cumulative factory methods for the builder pattern + * Mutable "setter" methods for the builder pattern. * * @author Jaroslav Tulach */ @@ -18,15 +18,21 @@ } public final ServerInfo nameProvider(NameProvider np) { - return new ServerInfo(np, this.url, this.reset, this.shutdown); + this.name = np; + return this; } public final ServerInfo urlProvider(URLProvider up) { - return new ServerInfo(this.name, up, this.reset, this.shutdown); + this.url = up; + return this; } + // BEGIN: aserverinfo.builder.setter public final ServerInfo reset(ResetHandler h) { - return new ServerInfo(this.name, this.url, h, this.shutdown); + this.reset = h; + return this; } + // END: aserverinfo.builder.setter + /** All one needs to do when there is a need to add new * style of creation is to add new method for a builder. * @param handler @@ -34,7 +40,8 @@ * @since 2.0 */ public final ServerInfo shutdown(ShutdownHandler handler) { - return new ServerInfo(this.name, this.url, this.reset, handler); + this.shutdown = handler; + return this; } /** Creates the server connector based on current values in the @@ -46,10 +53,10 @@ } // FINISH: aserverinfo.builder.factory - private final NameProvider name; - private final URLProvider url; - private final ResetHandler reset; - private final ShutdownHandler shutdown; + private NameProvider name; + private URLProvider url; + private ResetHandler reset; + private ShutdownHandler shutdown; private ServerInfo( NameProvider name, URLProvider url, diff -r 219810ff3c72 -r d5b6a877e5a8 samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java Sat Jun 20 16:06:19 2009 +0200 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java Tue Aug 25 11:32:10 2009 +0200 @@ -32,16 +32,16 @@ NameProvider np = p; URLProvider up = p; ResetHandler res = p; - ServerConnector inf; + ServerConnector connection; // BEGIN: ServerConnector.builder.creation - inf = ServerInfo.empty() + connection = ServerInfo.empty() .nameProvider(np).urlProvider(up).reset(res).connect(); // END: ServerConnector.builder.creation - assertEquals("API Design Server", inf.getName()); - assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm()); - inf.reset(); + assertEquals("API Design Server", connection.getName()); + assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm()); + connection.reset(); assertEquals("Once reset", 1, p.resets); } @@ -49,19 +49,19 @@ @Test public void showVerboseUseOfBuilder() throws Exception { Prov prov = new Prov(); - ServerConnector info; + ServerConnector connection; // BEGIN: ServerConnector.builder.creation.verbose ServerInfo empty = ServerInfo.empty(); ServerInfo name = empty.nameProvider(prov); ServerInfo urlAndName = name.urlProvider(prov); ServerInfo all = urlAndName.reset(prov); - info = all.connect(); + connection = all.connect(); // END: ServerConnector.builder.creation.verbose - assertEquals("API Design Server", info.getName()); - assertEquals("http://www.apidesign.org", info.getURL().toExternalForm()); - info.reset(); + assertEquals("API Design Server", connection.getName()); + assertEquals("http://www.apidesign.org", connection.getURL().toExternalForm()); + connection.reset(); assertEquals("Once reset", 1, prov.resets); }