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.proxy.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 proxy design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Provide a surrogate or placeholder for another object to control * access to it.&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;RealSubject&lt;/code&gt;s and &lt;code&gt;Proxy&lt;/code&gt;. * Both implement the &lt;code&gt;Subject&lt;/code&gt; interface. * * &lt;i&gt;Proxy&lt;/i&gt; forwards or blocks requests to &lt;i&gt;RealSubject&lt;/i&gt; * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * &lt;code&gt;Proxy&lt;/code&gt; needs to implement all methods of &lt;i&gt;Subject&lt;/i&gt;, even * those it is not interested in. &lt;i&gt;Proxy&lt;/i&gt; needs to be aware of its role * in the pattern. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Subject * @see RealSubject * @see Proxy */ public class Main { /** * Implements the driver for the proxy design pattern. Experimental setup: * The proxy is to count the number of calls to RealSubject.print(String). * It is not interested in calls to Subject.write(String). * Proxy has to implement all method of subject, even those that are of * no interest. */ public static void main (String[] args) { Subject real = new RealSubject(); Subject proxy = new Proxy(real); proxy.print(&quot;PRINT&quot;); proxy.write(&quot;WRITE&quot;); proxy.print(&quot;PRINT&quot;); proxy.write(&quot;WRITE&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 Proxy.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.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 a &lt;i&gt;Proxy&lt;/i&gt; according to GoF. It implements the * &lt;i&gt;Subject&lt;/i&gt; interface as &lt;i&gt;RealSubject&lt;/i&gt; does. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Subject * @see Proxy */ public class Proxy implements Subject { /** * a reference to the RealSubject for method forwarding */ private Subject realSubject; /** * An internal counter for the number of calls to &lt;code&gt; * print(String)&lt;/code&gt;. */ private int printCalls = 0; /** * Creates a new &lt;i&gt;Proxy&lt;/i&gt; with the given &lt;i&gt;RealSubject&lt;/i&gt;. * * @param subject The &lt;i&gt;RealSubject&lt;/i&gt; to forward method calls to */ public Proxy(Subject subject) { this.realSubject = subject; } /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;write(String)&lt;/code&gt; * * @param s the string to print */ public void print(String s) { printCalls++; realSubject.print(s); System.out.println(&quot;[Proxy:] That was call &quot;+printCalls+&quot; to Subject.print(String)&quot;); } /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;print(String)&lt;/code&gt; * * @param s the string to print */ public void write(String s) { realSubject.write(s); System.out.println(&quot;[Proxy:] Not interested in write calls, but must implement anyway&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 RealSubject.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.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 a &lt;i&gt;RealSubject&lt;/i&gt; according to GoF. It implements the * &lt;i&gt;Subject&lt;/i&gt; interface as &lt;i&gt;Proxy&lt;/i&gt; does. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Subject * @see Proxy */ public class RealSubject implements Subject { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;write(String)&lt;/code&gt; * * @param s the string to print */ public void print(String s) { System.out.println(&quot;[RealSubject.print:] &quot;+s); } /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;print(String)&lt;/code&gt; * * @param s the string to print */ public void write(String s) { System.out.println(&quot;[RealSubject.write:] &quot;+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 Subject.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.proxy.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 &lt;i&gt;Subject&lt;/i&gt; interface that is implemented by both &lt;i&gt;Proxy * &lt;/i&gt; and &lt;i&gt;RealSubject&lt;/i&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see RealSubject * @see Proxy */ public interface Subject { /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;write(String)&lt;/code&gt; * * @param s the string to print */ public void print(String s); /** * Prints the argument string to &lt;code&gt;System.out&lt;/code&gt;. This is * equivalent to &lt;code&gt;print(String)&lt;/code&gt; * * @param s the string to print */ public void write(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 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