277294.ijbsn.asia AbstractionImplementation.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Represents the &lt;i&gt;Abstraction&lt;/i&gt; in the scenario. &lt;code&gt;Screen&lt;/code&gt; * provides two methods to draw/print: &lt;code&gt;drawText(String)&lt;/code&gt; and * &lt;code&gt;drawTextBox(String)&lt;/code&gt;. The method implementations are provided * by an aspect. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public aspect AbstractionImplementation { /** * stores the actual &lt;i&gt;Implementor&lt;/i&gt; to use */ private ScreenImplementation Screen.implementor; /** * Draws or prints a text to an output device determined by the * current &lt;i&gt;Implementor&lt;/i&gt;. * * @param text The text to be drawn/printed */ public void Screen.drawText(String text) { implementor.printText(text); implementor.printLine(); } /** * Draws or prints a text in a box to an output device determined * by the current &lt;i&gt;Implementor&lt;/i&gt;. * * @param text The text to be drawn/printed */ public void Screen.drawTextBox(String text) { int length = text.length(); for(int i=0; i&lt;length+4; i++) { implementor.printDecor(); } implementor.printLine(); implementor.printDecor(); implementor.printText(&quot; &quot;+text+&quot; &quot;); implementor.printDecor(); implementor.printLine(); for(int i=0; i&lt;length+4; i++) { implementor.printDecor(); } implementor.printLine(); } /** * Sets the current &lt;i&gt;Implementor&lt;/i&gt;. * * @param implementor The new implementor */ public void Screen.setImplementor(ScreenImplementation implementor) { this.implementor = implementor; } } </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 CrossCapitalImplementation.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Prints capitalized text and uses the double cross (&quot;#&quot;) to decorate. * Represents a &lt;i&gt;ConcreteImplementation&lt;/i&gt; in the context of the Bridge * design pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public class CrossCapitalImplementation implements ScreenImplementation { /** * Does a line feed (to &lt;code&gt;System.out&lt;/code&gt;). */ public void printLine() { System.out.println(); } /** * Prints a double cross (&quot;#&quot;) to &lt;code&gt;System.out&lt;/code&gt;. */ public void printDecor() { System.out.print(&quot;X&quot;); } /** * Prints the argument text in capitals to &lt;code&gt;System.out&lt;/code&gt;. * * @param text the text to print */ public void printText(String text) { System.out.print(text.toUpperCase()); } } </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 GreetingScreen.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Prints/draws a greeting in a text box. Represents a &lt;i&gt;RefinedAbstraction * &lt;/i&gt; in the context of the Bridge design pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public class GreetingScreen extends Screen { /** * Creates a new &lt;code&gt;GreetingScreen&lt;/code&gt; object with the provided * &lt;i&gt;Implementor&lt;/i&gt;. * * @param si the implementor to use */ public GreetingScreen(ScreenImplementation si) { super(si); } /** * Draws/prints a greeting in a text box */ public void drawGreeting() { drawTextBox(&quot;Greetings!&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 InformationScreen.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Prints/draws the current system in a text box. Represents a * &lt;i&gt;RefinedAbstraction&lt;/i&gt; in the context of the Bridge design pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ import java.util.Date; public class InformationScreen extends Screen { /** * Creates a new &lt;code&gt;InformationScreen&lt;/code&gt; object with the provided * &lt;i&gt;Implementor&lt;/i&gt;. * * @param si the implementor to use */ public InformationScreen(ScreenImplementation si) { super(si); } /** * Draws/prints the system time in a text box */ public void drawInfo() { Date date = new Date(); drawTextBox(&quot;Current system time: &quot;+date); } } </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.bridge.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.patterns. * * Contributor(s): */ /** * Implements the driver for the Bridge design pattern example. &lt;p&gt; * * Intent: &lt;i&gt; Decouple an abstraction from its implementation so that the * two can vary independently.&lt;/i&gt;&lt;p&gt; * * Scenario: Have seperate hierarchies for Abstractions (here: Screens) * and Implementors (here: ScreenImplementation), so that both * can change independently of each other * * Participants: &lt;UL&gt; * * &lt;LI&gt; &lt;code&gt;Screen&lt;/code&gt; - &lt;i&gt;Abstract Abstraction&lt;/i&gt; that defines * an interface for printing text and boxedText to stdout. * &lt;LI&gt; &lt;code&gt;GreetingScreen&lt;/code&gt; - &lt;i&gt;RefinedAbstraction&lt;/i&gt; that prints * a boxed greeting message * &lt;LI&gt; &lt;code&gt;InformationScreen&lt;/code&gt; - &lt;i&gt;RefinedAbstraction&lt;/i&gt; that prints * the system time (boxed) * &lt;LI&gt; &lt;code&gt;ScreenImplementation&lt;/code&gt; - &lt;i&gt;Implementor&lt;/i&gt; interface, * defines basic operations to output formatted strings * &lt;LI&gt; &lt;code&gt;StarImplementation&lt;/code&gt; - &lt;i&gt;ConcreteImplementation&lt;/i&gt; that * creates textBoxes of stars * &lt;LI&gt; &lt;code&gt;CrossCapitalImplementation&lt;/code&gt; - &lt;i&gt;ConcreteImplementation * &lt;/i&gt; that creates textBoxes of double crosses (hashes) and prints all * text capitalized * &lt;/UL&gt;&lt;p&gt; * * &lt;i&gt;This is the Java implementation.&lt;/i&gt;&lt;p&gt; * * Note that &lt;i&gt;Abstraction&lt;/i&gt; cannot be an interface in Java, as we need to * specify how &lt;i&gt;operation()&lt;/i&gt; is performed using the interface of * &lt;i&gt;Implementor&lt;/i&gt;. As &lt;i&gt;Abstraction&lt;/i&gt; is not necessarily a defining * role, this is a limitation. With multiple inheritance, this would not be * the case. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Screen * @see InformationScreen * @see GreetingScreen * @see ScreenImplementation * @see StarImplementation * @see CrossCapitalImplementation */ public class Main { /** * Implements the driver for this example. The two different screens * and screen implementations are tested in all possible combinations. * * @param args required by Java, but ignored */ public static void main(String[] args) { System.out.println(&quot;Creating implementations...&quot;); ScreenImplementation i1 = new StarImplementation(); ScreenImplementation i2 = new CrossCapitalImplementation(); System.out.println(&quot;Creating abstraction (screens) / implementation combinations...&quot;); GreetingScreen gs1 = new GreetingScreen(i1); GreetingScreen gs2 = new GreetingScreen(i2); InformationScreen is1 = new InformationScreen(i1); InformationScreen is2 = new InformationScreen(i2); System.out.println(&quot;Starting test:\n&quot;); gs1.drawText(&quot;\nScreen 1 (Refined Abstraction 1, Implementation 1):&quot;); gs1.drawGreeting(); gs2.drawText(&quot;\nScreen 2 (Refined Abstraction 1, Implementation 2):&quot;); gs2.drawGreeting(); is1.drawText(&quot;\nScreen 3 (Refined Abstraction 2, Implementation 1):&quot;); is1.drawInfo(); is2.drawText(&quot;\nScreen 4 (Refined Abstraction 2, Implementation 2):&quot;); is2.drawInfo(); } }</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 Screen.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Represents the &lt;i&gt;Abstraction&lt;/i&gt; in the scenario. &lt;code&gt;Screen&lt;/code&gt; * provides two methods to draw/print: &lt;code&gt;drawText(String)&lt;/code&gt; and * &lt;code&gt;drawTextBox(String)&lt;/code&gt;. Both methods call appropriate methods * on the &lt;code&gt;ScreenImplementor&lt;/code&gt; of this &lt;code&gt;Screen&lt;/code&gt; object. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public class Screen { /** * stores the actual &lt;i&gt;Implementor&lt;/i&gt; to use */ private ScreenImplementation implementor; /** * Creates a new &lt;code&gt;Screen&lt;/code&gt; object given an &lt;i&gt;Implementor&lt;/i&gt; * * @param implementor the implementor to use for calls to * &lt;i&gt;operationImpl()&lt;/i&gt; */ public Screen(ScreenImplementation implementor) { this.implementor = implementor; } /** * Draws or prints a text to an output device determined by the * current &lt;i&gt;Implementor&lt;/i&gt;. * * @param text The text to be drawn/printed */ public void drawText(String text) { implementor.printText(text); implementor.printLine(); } /** * Draws or prints a text in a box to an output device determined * by the current &lt;i&gt;Implementor&lt;/i&gt;. * * @param text The text to be drawn/printed */ public void drawTextBox(String text) { int length = text.length(); for(int i=0; i&lt;length+4; i++) { implementor.printDecor(); } implementor.printLine(); implementor.printDecor(); implementor.printText(&quot; &quot;+text+&quot; &quot;); implementor.printDecor(); implementor.printLine(); for(int i=0; i&lt;length+4; i++) { implementor.printDecor(); } implementor.printLine(); } } </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 ScreenImplementation.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Prints lines, decorators and text. Represents a * &lt;i&gt;Implementor&lt;/i&gt; in the context of the Bridge design pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public interface ScreenImplementation { /** * Prints a line feed. */ void printLine(); /** * Prints a decorator symbol (a string of length 1). */ void printDecor(); /** * Prints the argument text. * * @param text the text to print */ void printText(String text); } </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 StarImplementation.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.bridge.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.patterns. * * Contributor(s): */ /** * Prints regular text and uses the star (&quot;*&quot;) to decorate. Represents a * &lt;i&gt;ConcreteImplementation&lt;/i&gt; in the context of the Bridge design pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public class StarImplementation implements ScreenImplementation { /** * Does a line feed (to &lt;code&gt;System.out&lt;/code&gt;). */ public void printLine() { System.out.println(); } /** * Prints a star (&quot;*&quot;) to &lt;code&gt;System.out&lt;/code&gt;. */ public void printDecor() { System.out.print(&quot;*&quot;); } /** * Prints the argument text to &lt;code&gt;System.out&lt;/code&gt;. * * @param text the text to print */ public void printText(String text) { System.out.print(text); } } </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