carfield.com.hkBracketDecorator.java2004-03-24T16:00:00Z2004-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 "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 decorator that adds brackets ("[", "]") 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("[");
output.print(s);
System.out.print("]");
}
/**
* 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:00ZConcreteOutput.java2004-03-24T16:00:00Z2004-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 "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 <i>Component</i> interface to print strings to <code>
* System.out</code>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*/
public class ConcreteOutput implements Output {
/**
* Prints the argument string to <code>System.out</code>.
*
* @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:00ZMain.java2004-03-24T16:00:00Z2004-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 "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 decorator design pattern example.<p>
*
* Intent: <i>Attach additional responsibilities to an object dynamically.
* Decorators provide a flexible alternative to subclassing for extending
* functionality.</i><p>
*
* Participating classes are <code>Output</code>s as <i>Component</i>s,
* <code>ConcreteOutput</code> as <i>ConcreteComponent</i>. The decorators
* are <code>OutputDecorator</code> as <i>Decorator</i>, and <code>
* StarDecorator</code> and <code>BracketDecorator</code> as <i>
* ConcreteDecorator</i>s.<p>
*
* Experimental setup: Concrete decorator (ConcreteOutput) prints a
* string, Decorators (StarDecorator and BracketDecorator) wrap other
* output around it. Output should be: "[ *** <String> *** ]"
*
* <p><i>This is the Java version.</i><p>
*
* 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.<p>
*
* Experimental setup: Concrete decorator (ConcreteOutput) prints a
* string, Decorators (StarDecorator and BracketDecorator) wrap other
* output around it. Output should be: "[ *** <String> *** ]"
*
* @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("<String>");
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:00ZOutput.java2004-03-24T16:00:00Z2004-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 "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 <i>Component</i> interface.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*/
public interface Output {
/**
* Prints the argument string to <code>System.out</code>.
*
* @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:00ZOutputDecorator.java2004-03-24T16:00:00Z2004-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 "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 <i>Decorator</i> interface. This is an abstract class to allow
* for default implementations (set varible "output", provide default
* implementation for <code>print(String)</code>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 06/13/02
*/
public abstract class OutputDecorator implements Output {
/**
* the <code>Output</code> to decorate
*/
protected Output output;
/**
* Prints the argument string to <code>System.out</code>. 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 <code>super(..)</code> 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:00ZStarDecorator.java2004-03-24T16:00:00Z2004-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 "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 decorator that adds stars (" *** ") 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(" *** ");
output.print(s);
System.out.print(" *** ");
}
/**
* 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: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