carfield.com.hk BracketDecorator.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.decorator.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 decorator that adds brackets (&quot;[&quot;, &quot;]&quot;) before and after the * string to decorate. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.1, 08/07/02 */ public class BracketDecorator extends OutputDecorator { /** * Adds brackets before and after the argument string before passing * the call on to the component this decorator decorates. * * @param s the string to be decorated. */ public void print(String s) { System.out.print(&quot;[&quot;); output.print(s); System.out.print(&quot;]&quot;); } /** * Creates a BracketDecorator for the given output component * * @param output the component to decorate. */ public BracketDecorator(Output output) { super(output); } }</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 ConcreteOutput.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.decorator.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 &lt;i&gt;Component&lt;/i&gt; interface to print strings to &lt;code&gt; * System.out&lt;/code&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 */ public class ConcreteOutput implements Output { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. * * @param s the string to be printed. */ public void print(String s) { System.out.print(s); } }</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.decorator.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 decorator design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Attach additional responsibilities to an object dynamically. * Decorators provide a flexible alternative to subclassing for extending * functionality.&lt;/i&gt;&lt;p&gt; * * Participating classes are &lt;code&gt;Output&lt;/code&gt;s as &lt;i&gt;Component&lt;/i&gt;s, * &lt;code&gt;ConcreteOutput&lt;/code&gt; as &lt;i&gt;ConcreteComponent&lt;/i&gt;. The decorators * are &lt;code&gt;OutputDecorator&lt;/code&gt; as &lt;i&gt;Decorator&lt;/i&gt;, and &lt;code&gt; * StarDecorator&lt;/code&gt; and &lt;code&gt;BracketDecorator&lt;/code&gt; as &lt;i&gt; * ConcreteDecorator&lt;/i&gt;s.&lt;p&gt; * * Experimental setup: Concrete decorator (ConcreteOutput) prints a * string, Decorators (StarDecorator and BracketDecorator) wrap other * output around it. Output should be: &quot;[ *** &lt;String&gt; *** ]&quot; * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * This version allows for dynamic composition of decorators. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see Component * @see CompositeA * @see LeafB */ public class Main { /** * Implements the driver for the decorator design pattern example.&lt;p&gt; * * Experimental setup: Concrete decorator (ConcreteOutput) prints a * string, Decorators (StarDecorator and BracketDecorator) wrap other * output around it. Output should be: &quot;[ *** &lt;String&gt; *** ]&quot; * * @param args command line paramters, unused */ public static void main(String[] args) { Output original = new ConcreteOutput(); Output stared = new StarDecorator(original); Output bracketed= new BracketDecorator(stared); bracketed.print(&quot;&lt;String&gt;&quot;); System.out.println(); } }</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 Output.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.decorator.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 &lt;i&gt;Component&lt;/i&gt; interface. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 */ public interface Output { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. * * @param s the string to be printed. */ public void print(String s); } </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 OutputDecorator.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.decorator.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 &lt;i&gt;Decorator&lt;/i&gt; interface. This is an abstract class to allow * for default implementations (set varible &quot;output&quot;, provide default * implementation for &lt;code&gt;print(String)&lt;/code&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 */ public abstract class OutputDecorator implements Output { /** * the &lt;code&gt;Output&lt;/code&gt; to decorate */ protected Output output; /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This method is * overwritten by concrete decorators. The default implementation * * @param s the string to be printed. */ public void print(String s) { output.print(s); } /** * Defines the constructor signature. Also provides a default * implementation so that concrete decorators don't have to * re-implement can just call &lt;code&gt;super(..)&lt;/code&gt; and don't have * to deal with setting the variable themselves. * * @param output the component to decorate. */ public OutputDecorator(Output output) { this.output = output; } }</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 StarDecorator.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.decorator.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 decorator that adds stars (&quot; *** &quot;) before and after the * string to decorate. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 */ public class StarDecorator extends OutputDecorator { /** * Adds three stars before and after the argument string before passing * the call on to the component this decorator decorates. * * @param s the string to be decorated. */ public void print(String s) { System.out.print(&quot; *** &quot;); output.print(s); System.out.print(&quot; *** &quot;); } /** * Creates a StarDecorator for the given output component * * @param output the component to decorate. */ public StarDecorator(Output output) { super(output); } }</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