277294.ijbsn.asiaMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.adapter.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 Adapter design pattern example. <p>
*
* Intent: <i> Convert the interface of a class into another interface clients
* expect. Adapter lets classes work together that couldn't otherwise because
* incompatible interfaces.</i><p>
*
* Experimental setup: <code>Screen</code> is the <i>Adaptee</i> that can print
* strings to <code>System.out</code>. <code>Printer</code> is the
* <i>Target</i> (interface), <code>PrinterScreenAdapter</code> is the
* <i>Adapter</i> that allows to access <code>Screen</code>'s functionality
* via <code>Printer</code>'s interface.<p>
*
* <i>This is the Java implementation.</i><p>
*
* The implementation is that of an <i>object adapter</i> (NOT class adapter),
* as the latter requires multiple inheritance which Java does not provide.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Printer
* @see Screen
* @see PrinterScreenAdapter
*/
public class Main {
/**
* the Adapter in the scenario
*/
private static Printer adapter;
/**
* the Adaptee in the scenario
*/
private static Screen screen;
/**
* Implements the driver. It creates a <code>PrinterScreenAdapter</code>
* that implements <code>Printer</code>'s interface and acts as an
* <i>Adapter</i> for a <code>Screen</code> object. It then calls
* <code>print(String)</code> on it. The <code>PrinterScreenAdapter</code>
* forwards the call to it's <code>Screen</code>'s <code>
* printToStdOut(String)</code> method.
*
* @param args required for a main method, but ignored
*/
public static void main(String[] args) {
System.out.println("Creating the Adaptee...");
screen = new Screen();
System.out.println("Creating the Adapter...");
adapter = new PrinterScreenAdapter(screen);
System.out.println("Adapter and Adaptee are the same object: "+(adapter.equals(screen)));
System.out.println("Issuing the request() to the Adapter...");
adapter.print("Test successful.");
}
}</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:00ZPrinter.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.adapter.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):
*/
/**
* Defines the target inteface with a general print method. Acts as the
* <i>Target</i> in the pattern context.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public interface Printer {
/**
* Prints the argument string. In the pattern context, this is the
* <i>request()</i> method on the <i>Target</i>. Implemented by
* PrinterScreenAdapter which acts as <i>Adapter</i>.
*
* @param s the string to print
* @see PrinterScreenAdapter
*/
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:00ZPrinterScreenAdapter.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.adapter.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 an <i>Object Adapter</i>. Implements the <i>Target</i> interface
* and stores a private variable of type <i>Adaptee</i> (here: <code>Screen
* </code>) to which it forwards appropriate method calls. <p>
* It is not possible to use a class adapter in Java as it requires multiple
* inheritance.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Printer
* @see Screen
*/
public class PrinterScreenAdapter implements Printer {
/**
* the adaptee to forward appropriate messages to.
*/
private Screen screen = new Screen();
/**
* Creates a new Adapter for a given Screen.
*
* @param screen the screen to adapt
*/
public PrinterScreenAdapter(Screen screen) {
this.screen = screen;
}
/**
* Implements the <i>Target</i> interface.
*
* @param s the string to print
* @see Printer#print(String)
* @see Screen#printToStdOut(String)
*/
public void print(String s) {
screen.printToStdOut(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:00ZScreen.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.adapter.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):
*/
/**
* Provides a specialized print method. Acts as the <i>Adaptee</i> in the
* pattern context.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class Screen {
/**
* Prints the argument string to <code>System.out</code>. This procedure
* will be adapted by the pattern. In the pattern context, this is the
* <i>specificRequest()</i> method on the <i>Adaptee</i>.
*
* @param s the string to be printed
* @see PrinterScreenAdapter#print(String) the adapted method
*/
public void printToStdOut(String s) {
System.out.println(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: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