277294.ijbsn.asia Main.java 2004-03-24T16:00:00Z 2004-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 &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 Adapter design pattern example. &lt;p&gt; * * Intent: &lt;i&gt; Convert the interface of a class into another interface clients * expect. Adapter lets classes work together that couldn't otherwise because * incompatible interfaces.&lt;/i&gt;&lt;p&gt; * * Experimental setup: &lt;code&gt;Screen&lt;/code&gt; is the &lt;i&gt;Adaptee&lt;/i&gt; that can print * strings to &lt;code&gt;System.out&lt;/code&gt;. &lt;code&gt;Printer&lt;/code&gt; is the * &lt;i&gt;Target&lt;/i&gt; (interface), &lt;code&gt;PrinterScreenAdapter&lt;/code&gt; is the * &lt;i&gt;Adapter&lt;/i&gt; that allows to access &lt;code&gt;Screen&lt;/code&gt;'s functionality * via &lt;code&gt;Printer&lt;/code&gt;'s interface.&lt;p&gt; * * &lt;i&gt;This is the Java implementation.&lt;/i&gt;&lt;p&gt; * * The implementation is that of an &lt;i&gt;object adapter&lt;/i&gt; (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 &lt;code&gt;PrinterScreenAdapter&lt;/code&gt; * that implements &lt;code&gt;Printer&lt;/code&gt;'s interface and acts as an * &lt;i&gt;Adapter&lt;/i&gt; for a &lt;code&gt;Screen&lt;/code&gt; object. It then calls * &lt;code&gt;print(String)&lt;/code&gt; on it. The &lt;code&gt;PrinterScreenAdapter&lt;/code&gt; * forwards the call to it's &lt;code&gt;Screen&lt;/code&gt;'s &lt;code&gt; * printToStdOut(String)&lt;/code&gt; method. * * @param args required for a main method, but ignored */ public static void main(String[] args) { System.out.println(&quot;Creating the Adaptee...&quot;); screen = new Screen(); System.out.println(&quot;Creating the Adapter...&quot;); adapter = new PrinterScreenAdapter(screen); System.out.println(&quot;Adapter and Adaptee are the same object: &quot;+(adapter.equals(screen))); System.out.println(&quot;Issuing the request() to the Adapter...&quot;); adapter.print(&quot;Test successful.&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 Printer.java 2004-03-24T16:00:00Z 2004-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 &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): */ /** * Defines the target inteface with a general print method. Acts as the * &lt;i&gt;Target&lt;/i&gt; 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 * &lt;i&gt;request()&lt;/i&gt; method on the &lt;i&gt;Target&lt;/i&gt;. Implemented by * PrinterScreenAdapter which acts as &lt;i&gt;Adapter&lt;/i&gt;. * * @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:00Z PrinterScreenAdapter.java 2004-03-24T16:00:00Z 2004-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 &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 an &lt;i&gt;Object Adapter&lt;/i&gt;. Implements the &lt;i&gt;Target&lt;/i&gt; interface * and stores a private variable of type &lt;i&gt;Adaptee&lt;/i&gt; (here: &lt;code&gt;Screen * &lt;/code&gt;) to which it forwards appropriate method calls. &lt;p&gt; * 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 &lt;i&gt;Target&lt;/i&gt; 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: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.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 &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): */ /** * Provides a specialized print method. Acts as the &lt;i&gt;Adaptee&lt;/i&gt; in the * pattern context. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 */ public class Screen { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This procedure * will be adapted by the pattern. In the pattern context, this is the * &lt;i&gt;specificRequest()&lt;/i&gt; method on the &lt;i&gt;Adaptee&lt;/i&gt;. * * @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: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