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 BuilderInstance.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.builder.aspectj; /* -*- 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 concrete builder design pattern instance. This aspect allows * to have the &lt;code&gt;Builder&lt;/code&gt; abstract class to become an interface, * without losing the possibility to attach default implementations and even * variables. * * This also illiustrates the tradeoffs: The current version of AspectJ * (1.0.4) does not allow protected introduction. To achieve the same result * as in the OO case, the result variable has to be introduced as public * (to be inherited). To make sure that no other classes can access that * variable, we define an error here that the compiler reports when other * classes try to access the result variable. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 07/13/02 * * @see Builder * @see TextBuilder * @see StructureBuilder */ public aspect BuilderInstance { /** * Attaches the result variable to the &lt;code&gt;Builder&lt;/code&gt; interface. */ public String Builder.result; /** * Attaches the method with a default implementation to the * &lt;code&gt;Builder&lt;/code&gt; interface. * * @returns the result string for the builder. */ public String Builder.getResult() { return result; } /** * Declares a compiler error that gets reported if other classes * (except Builders or this aspect) try to access the result variable. */ declare error: (set(public String Builder+.result) || get(public String Builder+.result)) &amp;&amp; ! (within(Builder+) || within(BuilderInstance)): &quot;variable result is aspect protected. &quot; +&quot;use getResult() to access it&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 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