277294.ijbsn.asiaBuilder.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>Builder</i>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 <i>buildPart()</i> operation for type parts.
*
* @param type the type to process
*/
public abstract void processType(String type);
/**
* Defines the <i>buildPart()</i> operation for attribute parts.
*
* @param type the type to process
*/
public abstract void processAttribute(String type);
/**
* Defines the <i>buildPart()</i> operation for value parts.
*
* @param type the type to process
*/
public abstract void processValue(String type);
/**
* Defines the <i>getResult()</i> operation for <i>Builder</i>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:00ZBuilderInstance.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Builder</code> 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 <code>Builder</code> interface.
*/
public String Builder.result;
/**
* Attaches the method with a default implementation to the
* <code>Builder</code> 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))
&& ! (within(Builder+)
|| within(BuilderInstance)):
"variable result is aspect protected. "
+"use getResult() to access it";
}
</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:00ZMain.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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.<p>
*
* Intent: <i>Separate the construction of a complex object from its
* representation so that the same construction process can create different
* representations</i><p>
*
* Participatng objects are <code>TextBuilder</code> and
* <code>StructureBuilder</code> as <i>Builder</i>s that implement the
* <code>Builder</code> interface.<p>
*
* In this example, <code>Main</code> acts as the <i>Director</i> that
* uses two different builders to build string representations of a
* person. <code>TextBuilder</code> creates a text-like representation,
* <code>StructureBuilder</code> a XML-like one.
*
* <p><i>This is the Java version.</i><p>
*
* In Java, the <i>Builder</i> has to be an abstract class (as opposed to
* an interface) to allow to define variables or default implementations
* there. Consequently, all <i>ConcreteBuilders</i> 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("Person");
builder.processAttribute("Name");
builder.processValue("James Brick");
builder.processAttribute("Age");
builder.processValue("33");
builder.processAttribute("Occupation");
builder.processValue("Builder");
}
/**
* Implements the driver for the Builder design pattern example.<p>
*
* In this example, <code>Main</code> acts as the <i>Director</i> that
* uses two different builders to build string representations of a
* person. <code>TextBuilder</code> creates a text-like representation,
* <code>StructureBuilder</code> 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:00ZStructureBuilder.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>ConcreteBuilder</i> 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 <i>buildPart()</i> operation for type parts.
*
* @param type the type to process
*/
public void processType(String newType) {
result = "<"+newType+">\n";
type = newType;
}
/**
* Defines the <i>buildPart()</i> operation for attribute parts.
*
* @param type the type to process
*/
public void processAttribute(String newAttribute) {
if (attribute != null) {
result += ("</" + attribute + ">\n");
}
result += ("\t<" + newAttribute + ">");
this.attribute = newAttribute;
}
/**
* Defines the <i>buildPart()</i> operation for value parts.
*
* @param type the type to process
*/
public void processValue(String type) {
result += (type);
}
/**
* Defines the <i>getResult()</i> operation for <i>Builder</i>s. Includes
* a default implementation.
*
* @param type the type to process
*/
public String getResult() {
if (attribute != null) {
result += ("</" + attribute + ">\n");
attribute = null;
}
if (type != null) {
result += ("</" + type + ">\n");
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:00ZTextBuilder.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <i>ConcreteBuilder</i> that builds textual descriptions
* of complex objects consisting of type, attributes and values. This verison
* makes use of the default implementation of <code>getResult()</code>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class TextBuilder extends Builder {
/**
* Defines the <i>buildPart()</i> operation for type parts.
*
* @param type the type to process
*/
public void processType(String type) {
result = "This is a new "+type+":\n";
}
/**
* Defines the <i>buildPart()</i> operation for attribute parts.
*
* @param type the type to process
*/
public void processAttribute(String attribute) {
result += ("Its " + attribute + " is ");
}
/**
* Defines the <i>buildPart()</i> operation for value parts.
*
* @param type the type to process
*/
public void processValue(String type) {
result += (type + ".\n");
}
}
</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:00Zfiles.lst2004-03-24T16:00:00Z2004-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