277294.ijbsn.asia Builder.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.builder.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.pattern. * * Contributor(s): */ /** * Defines the interface for &lt;i&gt;Builder&lt;/i&gt;s. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see TextBuilder * @see StructureBuilder */ public abstract class Builder { /** * An inheritable variable that carries the result of the build. */ protected String result; /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for type parts. * * @param type the type to process */ public abstract void processType(String type); /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for attribute parts. * * @param type the type to process */ public abstract void processAttribute(String type); /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for value parts. * * @param type the type to process */ public abstract void processValue(String type); /** * Defines the &lt;i&gt;getResult()&lt;/i&gt; operation for &lt;i&gt;Builder&lt;/i&gt;s. Includes * a default implementation. * * @param type the type to process */ public String getResult() { return result; } } </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> 2004-03-24T16:00:00Z Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.builder.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.pattern. * * Contributor(s): */ /** * Implements the driver for the Builder design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Separate the construction of a complex object from its * representation so that the same construction process can create different * representations&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;TextBuilder&lt;/code&gt; and * &lt;code&gt;StructureBuilder&lt;/code&gt; as &lt;i&gt;Builder&lt;/i&gt;s that implement the * &lt;code&gt;Builder&lt;/code&gt; interface.&lt;p&gt; * * In this example, &lt;code&gt;Main&lt;/code&gt; acts as the &lt;i&gt;Director&lt;/i&gt; that * uses two different builders to build string representations of a * person. &lt;code&gt;TextBuilder&lt;/code&gt; creates a text-like representation, * &lt;code&gt;StructureBuilder&lt;/code&gt; a XML-like one. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * In Java, the &lt;i&gt;Builder&lt;/i&gt; has to be an abstract class (as opposed to * an interface) to allow to define variables or default implementations * there. Consequently, all &lt;i&gt;ConcreteBuilders&lt;/i&gt; have to have that * class as their superclass, making it impossible to be part of another * class hierarchy. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Builder * @see TextBuilder * @see StructureBuilder */ public class Main { /** * Builds a string representation of a person using a given builder. * * @param the builder to use. */ protected static void build(Builder builder) { builder.processType(&quot;Person&quot;); builder.processAttribute(&quot;Name&quot;); builder.processValue(&quot;James Brick&quot;); builder.processAttribute(&quot;Age&quot;); builder.processValue(&quot;33&quot;); builder.processAttribute(&quot;Occupation&quot;); builder.processValue(&quot;Builder&quot;); } /** * Implements the driver for the Builder design pattern example.&lt;p&gt; * * In this example, &lt;code&gt;Main&lt;/code&gt; acts as the &lt;i&gt;Director&lt;/i&gt; that * uses two different builders to build string representations of a * person. &lt;code&gt;TextBuilder&lt;/code&gt; creates a text-like representation, * &lt;code&gt;StructureBuilder&lt;/code&gt; a XML-like one. * * @param args the command-line parameters, unused. * */ public static void main(String[] args) { Builder builder1 = new TextBuilder(); Builder builder2 = new StructureBuilder(); build(builder1); build(builder2); System.out.println(builder1.getResult()); System.out.println(builder2.getResult()); } }</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> 2004-03-24T16:00:00Z StructureBuilder.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.builder.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.pattern. * * Contributor(s): */ /** * Implements a &lt;i&gt;ConcreteBuilder&lt;/i&gt; that builds XML descriptions * of complex objects consisting of type, attributes and values. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 */ public class StructureBuilder extends Builder { protected String type = null; protected String attribute = null; /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for type parts. * * @param type the type to process */ public void processType(String newType) { result = &quot;&lt;&quot;+newType+&quot;&gt;\n&quot;; type = newType; } /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for attribute parts. * * @param type the type to process */ public void processAttribute(String newAttribute) { if (attribute != null) { result += (&quot;&lt;/&quot; + attribute + &quot;&gt;\n&quot;); } result += (&quot;\t&lt;&quot; + newAttribute + &quot;&gt;&quot;); this.attribute = newAttribute; } /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for value parts. * * @param type the type to process */ public void processValue(String type) { result += (type); } /** * Defines the &lt;i&gt;getResult()&lt;/i&gt; operation for &lt;i&gt;Builder&lt;/i&gt;s. Includes * a default implementation. * * @param type the type to process */ public String getResult() { if (attribute != null) { result += (&quot;&lt;/&quot; + attribute + &quot;&gt;\n&quot;); attribute = null; } if (type != null) { result += (&quot;&lt;/&quot; + type + &quot;&gt;\n&quot;); type = null; } return result; } } </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> 2004-03-24T16:00:00Z TextBuilder.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.builder.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.pattern. * * Contributor(s): */ /** * Implements a &lt;i&gt;ConcreteBuilder&lt;/i&gt; that builds textual descriptions * of complex objects consisting of type, attributes and values. This verison * makes use of the default implementation of &lt;code&gt;getResult()&lt;/code&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 */ public class TextBuilder extends Builder { /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for type parts. * * @param type the type to process */ public void processType(String type) { result = &quot;This is a new &quot;+type+&quot;:\n&quot;; } /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for attribute parts. * * @param type the type to process */ public void processAttribute(String attribute) { result += (&quot;Its &quot; + attribute + &quot; is &quot;); } /** * Defines the &lt;i&gt;buildPart()&lt;/i&gt; operation for value parts. * * @param type the type to process */ public void processValue(String type) { result += (type + &quot;.\n&quot;); } } </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> 2004-03-24T16:00:00Z files.lst 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <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> 2004-03-24T16:00:00Z