Adding builder pattern
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 08:19:20 +0100
changeset 2940753acb192c7
parent 293 4feb55fa3c91
child 295 d6fa728fb2ba
Adding builder pattern
samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerConnector.java
samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java
samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java
samples/aserverinfo/test/org/apidesign/aserverinfo/test/CummulativeFactoryTest.java
samples/aserverinfo/test/org/apidesign/aserverinfo/test/ServerConnectorTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerConnector.java	Sat Nov 15 08:19:20 2008 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +package org.apidesign.aserverinfo.builder;
     1.5 +
     1.6 +import org.apidesign.aserverinfo.spi.NameProvider;
     1.7 +import org.apidesign.aserverinfo.spi.ResetHandler;
     1.8 +import org.apidesign.aserverinfo.spi.URLProvider;
     1.9 +import java.net.URL;
    1.10 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
    1.11 +
    1.12 +// BEGIN: aserverinfo.builder.api
    1.13 +public final class ServerConnector {
    1.14 +    public String getName() {
    1.15 +        return name == null ? "noname" : name.getName();
    1.16 +    }
    1.17 +    
    1.18 +    public URL getURL() {
    1.19 +        return url == null ? null : url.getURL();
    1.20 +    }
    1.21 +    
    1.22 +    public void reset() {
    1.23 +        if (reset != null) {
    1.24 +            reset.reset();
    1.25 +        }
    1.26 +    }
    1.27 +
    1.28 +    /** Additional method for API clients not available from first version.
    1.29 +     * @since 2.0
    1.30 +     */
    1.31 +    public void shutdown() {
    1.32 +        if (shutdown != null) {
    1.33 +            shutdown.shutdown();
    1.34 +        }
    1.35 +    }
    1.36 +// FINISH: aserverinfo.builder.api
    1.37 +
    1.38 +    //
    1.39 +    // private part
    1.40 +    //
    1.41 +
    1.42 +    private final NameProvider name;
    1.43 +    private final URLProvider url;
    1.44 +    private final ResetHandler reset;
    1.45 +    private final ShutdownHandler shutdown;
    1.46 +
    1.47 +    ServerConnector(
    1.48 +        NameProvider name, URLProvider url,
    1.49 +        ResetHandler reset, ShutdownHandler shutdown
    1.50 +    ) {
    1.51 +        this.name = name;
    1.52 +        this.url = url;
    1.53 +        this.reset = reset;
    1.54 +        this.shutdown = shutdown;
    1.55 +    }
    1.56 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/builder/ServerInfo.java	Sat Nov 15 08:19:20 2008 +0100
     2.3 @@ -0,0 +1,67 @@
     2.4 +package org.apidesign.aserverinfo.builder;
     2.5 +
     2.6 +import org.apidesign.aserverinfo.spi.NameProvider;
     2.7 +import org.apidesign.aserverinfo.spi.ResetHandler;
     2.8 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
     2.9 +import org.apidesign.aserverinfo.spi.URLProvider;
    2.10 +
    2.11 +/**
    2.12 + *
    2.13 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.14 + */
    2.15 +public class ServerInfo {
    2.16 +
    2.17 +    //
    2.18 +    // cumulative factory methods for the builder pattern
    2.19 +    //
    2.20 +
    2.21 +    // BEGIN: aserverinfo.builder.factory
    2.22 +    public static ServerInfo empty() {
    2.23 +        return new ServerInfo(null, null, null, null);
    2.24 +    }
    2.25 +
    2.26 +    public final ServerInfo nameProvider(NameProvider np) {
    2.27 +        return new ServerInfo(np, this.url, this.reset, this.shutdown);
    2.28 +    }
    2.29 +
    2.30 +    public final ServerInfo urlProvider(URLProvider up) {
    2.31 +        return new ServerInfo(this.name, up, this.reset, this.shutdown);
    2.32 +    }
    2.33 +    public final ServerInfo reset(ResetHandler h) {
    2.34 +        return new ServerInfo(this.name, this.url, h, this.shutdown);
    2.35 +    }
    2.36 +    /** All one needs to do when there is a need to add new
    2.37 +     * style of creation is to add new method for a builder.
    2.38 +     * @param handler
    2.39 +     * @return
    2.40 +     * @since 2.0
    2.41 +     */
    2.42 +    public final ServerInfo shutdown(ShutdownHandler handler) {
    2.43 +        return new ServerInfo(this.name, this.url, this.reset, handler);
    2.44 +    }
    2.45 +    
    2.46 +    /** Creates the server connector based on current values in the 
    2.47 +     * info. Builder factory method.
    2.48 +     * @return server connector
    2.49 +     */
    2.50 +    public final ServerConnector connect() {
    2.51 +        return new ServerConnector(name, url, reset, shutdown);
    2.52 +    }
    2.53 +    // END: aserverinfo.builder.factory
    2.54 +
    2.55 +    private final NameProvider name;
    2.56 +    private final URLProvider url;
    2.57 +    private final ResetHandler reset;
    2.58 +    private final ShutdownHandler shutdown;
    2.59 +
    2.60 +    private ServerInfo(
    2.61 +        NameProvider name, URLProvider url,
    2.62 +        ResetHandler reset, ShutdownHandler shutdown
    2.63 +    ) {
    2.64 +        this.name = name;
    2.65 +        this.url = url;
    2.66 +        this.reset = reset;
    2.67 +        this.shutdown = shutdown;
    2.68 +    }
    2.69 +
    2.70 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/BuilderFactoryTest.java	Sat Nov 15 08:19:20 2008 +0100
     3.3 @@ -0,0 +1,92 @@
     3.4 +package org.apidesign.aserverinfo.test;
     3.5 +
     3.6 +import java.net.MalformedURLException;
     3.7 +import java.net.URL;
     3.8 +import org.apidesign.aserverinfo.builder.ServerConnector;
     3.9 +import org.apidesign.aserverinfo.builder.ServerInfo;
    3.10 +import org.apidesign.aserverinfo.spi.NameProvider;
    3.11 +import org.apidesign.aserverinfo.spi.ResetHandler;
    3.12 +import org.apidesign.aserverinfo.spi.URLProvider;
    3.13 +import org.junit.After;
    3.14 +import org.junit.Before;
    3.15 +import org.junit.Test;
    3.16 +import static org.junit.Assert.*;
    3.17 +import org.openide.util.Exceptions;
    3.18 +
    3.19 +public class BuilderFactoryTest {
    3.20 +
    3.21 +    public BuilderFactoryTest() {
    3.22 +    }
    3.23 +
    3.24 +    @Before
    3.25 +    public void setUp() {
    3.26 +    }
    3.27 +
    3.28 +    @After
    3.29 +    public void tearDown() {
    3.30 +    }
    3.31 +
    3.32 +    @Test
    3.33 +    public void showUseOfCumulativeFactory() throws Exception {
    3.34 +        Prov p = new Prov();
    3.35 +        NameProvider np = p;
    3.36 +        URLProvider up = p;
    3.37 +        ResetHandler res = p;
    3.38 +        ServerConnector inf;
    3.39 +        
    3.40 +        // BEGIN: ServerConnector.cumulative.creation
    3.41 +        inf = ServerInfo.empty()
    3.42 +                .nameProvider(np).urlProvider(up).reset(res).connect();
    3.43 +        // END: ServerConnector.cumulative.creation
    3.44 +        
    3.45 +        assertEquals("API Design Server", inf.getName());
    3.46 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    3.47 +        inf.reset();
    3.48 +        assertEquals("Once reset", 1, p.resets);
    3.49 +        
    3.50 +    }
    3.51 +    
    3.52 +    @Test
    3.53 +    public void showVerboseUseOfCumulativeFactory() throws Exception {
    3.54 +        Prov prov = new Prov();
    3.55 +        ServerConnector info;
    3.56 +        
    3.57 +        // BEGIN: ServerConnector.builder.creation.verbose
    3.58 +        ServerInfo empty = ServerInfo.empty();
    3.59 +        ServerInfo name = empty.nameProvider(prov);
    3.60 +        ServerInfo urlAndName = name.urlProvider(prov);
    3.61 +        ServerInfo all = urlAndName.reset(prov);
    3.62 +        info = all.connect();
    3.63 +        // END: ServerConnector.builder.creation.verbose
    3.64 +        
    3.65 +        assertEquals("API Design Server", info.getName());
    3.66 +        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    3.67 +        info.reset();
    3.68 +        assertEquals("Once reset", 1, prov.resets);
    3.69 +        
    3.70 +    }
    3.71 +    
    3.72 +    
    3.73 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    3.74 +        int resets;
    3.75 +
    3.76 +        public String getName() {
    3.77 +            return "API Design Server";
    3.78 +        }
    3.79 +
    3.80 +        public URL getURL() {
    3.81 +            try {
    3.82 +                return new URL("http://www.apidesign.org");
    3.83 +            } catch (MalformedURLException ex) {
    3.84 +                Exceptions.printStackTrace(ex);
    3.85 +                return null;
    3.86 +            }
    3.87 +        }
    3.88 +
    3.89 +        public void reset() {
    3.90 +            resets++;
    3.91 +        }
    3.92 +
    3.93 +    }
    3.94 +        
    3.95 +}
    3.96 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/CummulativeFactoryTest.java	Sat Nov 15 08:19:20 2008 +0100
     4.3 @@ -0,0 +1,89 @@
     4.4 +package org.apidesign.aserverinfo.test;
     4.5 +
     4.6 +import java.net.MalformedURLException;
     4.7 +import java.net.URL;
     4.8 +import org.apidesign.aserverinfo.cummulativefactory.ServerConnector;
     4.9 +import org.apidesign.aserverinfo.spi.NameProvider;
    4.10 +import org.apidesign.aserverinfo.spi.ResetHandler;
    4.11 +import org.apidesign.aserverinfo.spi.URLProvider;
    4.12 +import org.junit.After;
    4.13 +import org.junit.Before;
    4.14 +import org.junit.Test;
    4.15 +import static org.junit.Assert.*;
    4.16 +import org.openide.util.Exceptions;
    4.17 +
    4.18 +public class CummulativeFactoryTest {
    4.19 +
    4.20 +    public CummulativeFactoryTest() {
    4.21 +    }
    4.22 +
    4.23 +    @Before
    4.24 +    public void setUp() {
    4.25 +    }
    4.26 +
    4.27 +    @After
    4.28 +    public void tearDown() {
    4.29 +    }
    4.30 +
    4.31 +    @Test
    4.32 +    public void showUseOfCumulativeFactory() throws Exception {
    4.33 +        Prov p = new Prov();
    4.34 +        NameProvider np = p;
    4.35 +        URLProvider up = p;
    4.36 +        ResetHandler res = p;
    4.37 +        ServerConnector inf;
    4.38 +        
    4.39 +        // BEGIN: ServerConnector.cumulative.creation
    4.40 +        inf = ServerConnector.empty().nameProvider(np).urlProvider(up).reset(res);
    4.41 +        // END: ServerConnector.cumulative.creation
    4.42 +        
    4.43 +        assertEquals("API Design Server", inf.getName());
    4.44 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    4.45 +        inf.reset();
    4.46 +        assertEquals("Once reset", 1, p.resets);
    4.47 +        
    4.48 +    }
    4.49 +    
    4.50 +    @Test
    4.51 +    public void showVerboseUseOfCumulativeFactory() throws Exception {
    4.52 +        Prov prov = new Prov();
    4.53 +        ServerConnector info;
    4.54 +        
    4.55 +        // BEGIN: ServerConnector.cumulative.creation.verbose
    4.56 +        ServerConnector empty = ServerConnector.empty();
    4.57 +        ServerConnector name = empty.nameProvider(prov);
    4.58 +        ServerConnector urlAndName = name.urlProvider(prov);
    4.59 +        info = urlAndName.reset(prov);
    4.60 +        // END: ServerConnector.cumulative.creation.verbose
    4.61 +        
    4.62 +        assertEquals("API Design Server", info.getName());
    4.63 +        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    4.64 +        info.reset();
    4.65 +        assertEquals("Once reset", 1, prov.resets);
    4.66 +        
    4.67 +    }
    4.68 +    
    4.69 +    
    4.70 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    4.71 +        int resets;
    4.72 +
    4.73 +        public String getName() {
    4.74 +            return "API Design Server";
    4.75 +        }
    4.76 +
    4.77 +        public URL getURL() {
    4.78 +            try {
    4.79 +                return new URL("http://www.apidesign.org");
    4.80 +            } catch (MalformedURLException ex) {
    4.81 +                Exceptions.printStackTrace(ex);
    4.82 +                return null;
    4.83 +            }
    4.84 +        }
    4.85 +
    4.86 +        public void reset() {
    4.87 +            resets++;
    4.88 +        }
    4.89 +
    4.90 +    }
    4.91 +        
    4.92 +}
    4.93 \ No newline at end of file
     5.1 --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/test/ServerConnectorTest.java	Sat Nov 15 06:57:15 2008 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,89 +0,0 @@
     5.4 -package org.apidesign.aserverinfo.test;
     5.5 -
     5.6 -import java.net.MalformedURLException;
     5.7 -import java.net.URL;
     5.8 -import org.apidesign.aserverinfo.cummulativefactory.ServerConnector;
     5.9 -import org.apidesign.aserverinfo.spi.NameProvider;
    5.10 -import org.apidesign.aserverinfo.spi.ResetHandler;
    5.11 -import org.apidesign.aserverinfo.spi.URLProvider;
    5.12 -import org.junit.After;
    5.13 -import org.junit.Before;
    5.14 -import org.junit.Test;
    5.15 -import static org.junit.Assert.*;
    5.16 -import org.openide.util.Exceptions;
    5.17 -
    5.18 -public class ServerConnectorTest {
    5.19 -
    5.20 -    public ServerConnectorTest() {
    5.21 -    }
    5.22 -
    5.23 -    @Before
    5.24 -    public void setUp() {
    5.25 -    }
    5.26 -
    5.27 -    @After
    5.28 -    public void tearDown() {
    5.29 -    }
    5.30 -
    5.31 -    @Test
    5.32 -    public void showUseOfCumulativeFactory() throws Exception {
    5.33 -        Prov p = new Prov();
    5.34 -        NameProvider np = p;
    5.35 -        URLProvider up = p;
    5.36 -        ResetHandler res = p;
    5.37 -        ServerConnector inf;
    5.38 -        
    5.39 -        // BEGIN: ServerConnector.cumulative.creation
    5.40 -        inf = ServerConnector.empty().nameProvider(np).urlProvider(up).reset(res);
    5.41 -        // END: ServerConnector.cumulative.creation
    5.42 -        
    5.43 -        assertEquals("API Design Server", inf.getName());
    5.44 -        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
    5.45 -        inf.reset();
    5.46 -        assertEquals("Once reset", 1, p.resets);
    5.47 -        
    5.48 -    }
    5.49 -    
    5.50 -    @Test
    5.51 -    public void showVerboseUseOfCumulativeFactory() throws Exception {
    5.52 -        Prov prov = new Prov();
    5.53 -        ServerConnector info;
    5.54 -        
    5.55 -        // BEGIN: ServerConnector.cumulative.creation.verbose
    5.56 -        ServerConnector empty = ServerConnector.empty();
    5.57 -        ServerConnector name = empty.nameProvider(prov);
    5.58 -        ServerConnector urlAndName = name.urlProvider(prov);
    5.59 -        info = urlAndName.reset(prov);
    5.60 -        // END: ServerConnector.cumulative.creation.verbose
    5.61 -        
    5.62 -        assertEquals("API Design Server", info.getName());
    5.63 -        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
    5.64 -        info.reset();
    5.65 -        assertEquals("Once reset", 1, prov.resets);
    5.66 -        
    5.67 -    }
    5.68 -    
    5.69 -    
    5.70 -    private static class Prov implements NameProvider, URLProvider, ResetHandler {
    5.71 -        int resets;
    5.72 -
    5.73 -        public String getName() {
    5.74 -            return "API Design Server";
    5.75 -        }
    5.76 -
    5.77 -        public URL getURL() {
    5.78 -            try {
    5.79 -                return new URL("http://www.apidesign.org");
    5.80 -            } catch (MalformedURLException ex) {
    5.81 -                Exceptions.printStackTrace(ex);
    5.82 -                return null;
    5.83 -            }
    5.84 -        }
    5.85 -
    5.86 -        public void reset() {
    5.87 -            resets++;
    5.88 -        }
    5.89 -
    5.90 -    }
    5.91 -        
    5.92 -}
    5.93 \ No newline at end of file