277294.ijbsn.asiaAbstractionImplementation.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Represents the <i>Abstraction</i> in the scenario. <code>Screen</code>
* provides two methods to draw/print: <code>drawText(String)</code> and
* <code>drawTextBox(String)</code>. 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 <i>Implementor</i> to use
*/
private ScreenImplementation Screen.implementor;
/**
* Draws or prints a text to an output device determined by the
* current <i>Implementor</i>.
*
* @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 <i>Implementor</i>.
*
* @param text The text to be drawn/printed
*/
public void Screen.drawTextBox(String text) {
int length = text.length();
for(int i=0; i<length+4; i++) {
implementor.printDecor();
}
implementor.printLine();
implementor.printDecor();
implementor.printText(" "+text+" ");
implementor.printDecor();
implementor.printLine();
for(int i=0; i<length+4; i++) {
implementor.printDecor();
}
implementor.printLine();
}
/**
* Sets the current <i>Implementor</i>.
*
* @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:00ZCrossCapitalImplementation.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Prints capitalized text and uses the double cross ("#") to decorate.
* Represents a <i>ConcreteImplementation</i> 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 <code>System.out</code>).
*/
public void printLine() {
System.out.println();
}
/**
* Prints a double cross ("#") to <code>System.out</code>.
*/
public void printDecor() {
System.out.print("X");
}
/**
* Prints the argument text in capitals to <code>System.out</code>.
*
* @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:00ZGreetingScreen.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Prints/draws a greeting in a text box. Represents a <i>RefinedAbstraction
* </i> 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 <code>GreetingScreen</code> object with the provided
* <i>Implementor</i>.
*
* @param si the implementor to use
*/
public GreetingScreen(ScreenImplementation si) {
super(si);
}
/**
* Draws/prints a greeting in a text box
*/
public void drawGreeting() {
drawTextBox("Greetings!");
}
}
</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:00ZInformationScreen.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Prints/draws the current system in a text box. Represents a
* <i>RefinedAbstraction</i> 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 <code>InformationScreen</code> object with the provided
* <i>Implementor</i>.
*
* @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("Current system time: "+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:00ZMain.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Implements the driver for the Bridge design pattern example. <p>
*
* Intent: <i> Decouple an abstraction from its implementation so that the
* two can vary independently.</i><p>
*
* Scenario: Have seperate hierarchies for Abstractions (here: Screens)
* and Implementors (here: ScreenImplementation), so that both
* can change independently of each other
*
* Participants: <UL>
*
* <LI> <code>Screen</code> - <i>Abstract Abstraction</i> that defines
* an interface for printing text and boxedText to stdout.
* <LI> <code>GreetingScreen</code> - <i>RefinedAbstraction</i> that prints
* a boxed greeting message
* <LI> <code>InformationScreen</code> - <i>RefinedAbstraction</i> that prints
* the system time (boxed)
* <LI> <code>ScreenImplementation</code> - <i>Implementor</i> interface,
* defines basic operations to output formatted strings
* <LI> <code>StarImplementation</code> - <i>ConcreteImplementation</i> that
* creates textBoxes of stars
* <LI> <code>CrossCapitalImplementation</code> - <i>ConcreteImplementation
* </i> that creates textBoxes of double crosses (hashes) and prints all
* text capitalized
* </UL><p>
*
* <i>This is the Java implementation.</i><p>
*
* Note that <i>Abstraction</i> cannot be an interface in Java, as we need to
* specify how <i>operation()</i> is performed using the interface of
* <i>Implementor</i>. As <i>Abstraction</i> 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("Creating implementations...");
ScreenImplementation i1 = new StarImplementation();
ScreenImplementation i2 = new CrossCapitalImplementation();
System.out.println("Creating abstraction (screens) / implementation combinations...");
GreetingScreen gs1 = new GreetingScreen(i1);
GreetingScreen gs2 = new GreetingScreen(i2);
InformationScreen is1 = new InformationScreen(i1);
InformationScreen is2 = new InformationScreen(i2);
System.out.println("Starting test:\n");
gs1.drawText("\nScreen 1 (Refined Abstraction 1, Implementation 1):");
gs1.drawGreeting();
gs2.drawText("\nScreen 2 (Refined Abstraction 1, Implementation 2):");
gs2.drawGreeting();
is1.drawText("\nScreen 3 (Refined Abstraction 2, Implementation 1):");
is1.drawInfo();
is2.drawText("\nScreen 4 (Refined Abstraction 2, Implementation 2):");
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:00ZScreen.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Represents the <i>Abstraction</i> in the scenario. <code>Screen</code>
* provides two methods to draw/print: <code>drawText(String)</code> and
* <code>drawTextBox(String)</code>. Both methods call appropriate methods
* on the <code>ScreenImplementor</code> of this <code>Screen</code> object.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
*/
public class Screen {
/**
* stores the actual <i>Implementor</i> to use
*/
private ScreenImplementation implementor;
/**
* Creates a new <code>Screen</code> object given an <i>Implementor</i>
*
* @param implementor the implementor to use for calls to
* <i>operationImpl()</i>
*/
public Screen(ScreenImplementation implementor) {
this.implementor = implementor;
}
/**
* Draws or prints a text to an output device determined by the
* current <i>Implementor</i>.
*
* @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 <i>Implementor</i>.
*
* @param text The text to be drawn/printed
*/
public void drawTextBox(String text) {
int length = text.length();
for(int i=0; i<length+4; i++) {
implementor.printDecor();
}
implementor.printLine();
implementor.printDecor();
implementor.printText(" "+text+" ");
implementor.printDecor();
implementor.printLine();
for(int i=0; i<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:00ZScreenImplementation.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Prints lines, decorators and text. Represents a
* <i>Implementor</i> 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:00ZStarImplementation.java2004-03-24T16:00:00Z2004-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 "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.patterns.
*
* Contributor(s):
*/
/**
* Prints regular text and uses the star ("*") to decorate. Represents a
* <i>ConcreteImplementation</i> 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 <code>System.out</code>).
*/
public void printLine() {
System.out.println();
}
/**
* Prints a star ("*") to <code>System.out</code>.
*/
public void printDecor() {
System.out.print("*");
}
/**
* Prints the argument text to <code>System.out</code>.
*
* @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: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