carfield.com.hk 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.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): */ /** * 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 AspectJ implementation.&lt;/i&gt;&lt;p&gt; * * The implementation is that of an &lt;i&gt;object adapter&lt;/i&gt; (NOT class adapter). * The aspect &lt;code&gt;PrinterScreenAdapter&lt;/code&gt; attaches the necessary method * to &lt;code&gt;Screen&lt;/code&gt; so that the &lt;i&gt;Adaptee&lt;/i&gt; becomes its own * &lt;i&gt;Adapter&lt;/i&gt;. &lt;p&gt; * * While now &lt;i&gt;Adaptee&lt;/i&gt; and &lt;i&gt;Adapter&lt;/i&gt; are of the same type, this * approach only works if &lt;i&gt;request()&lt;/i&gt; and &lt;i&gt;specificRequest()&lt;/i&gt; do not * have the same signature or same signature except for return type. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Printer * @see Screen * @see PrinterScreenAdapter */ public class Main { /** * 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 (which is its own Adapter)...&quot;); screen = new Screen(); System.out.println(&quot;Adapter and Adaptee are the same object: &quot;+(screen == screen)); System.out.println(&quot;Issuing the request() to the Adapter...&quot;); screen.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.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): */ /** * 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.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 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 aspect PrinterScreenAdapter { /** * Extends the interface of &lt;code&gt;Screen&lt;/code&gt;. This means that * &lt;i&gt;Adaptee&lt;/i&gt; becomes its own &lt;i&gt;Adapter&lt;/i&gt;. */ declare parents: Screen implements Printer; /** * Attaches a &lt;code&gt;print()&lt;/code&gt; method to &lt;code&gt;Screen&lt;/code&gt;. * This means that &lt;i&gt;Adaptee&lt;/i&gt; now implements &lt;i&gt;request()&lt;/i&gt;. * * @param s the string to print * @see Printer#print(String) * @see Screen#printToStdOut(String) */ public void Screen.print(String s) { 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.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): */ /** * 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