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.singleton.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 Singleton design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Ensure that a class has only one instance and provide a global * point of access to it.&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;PrinterSingleton&lt;/code&gt; p1, p2, p3 and * &lt;code&gt;PrinterSubclass&lt;/code&gt; ps1, ps2, ps3.&lt;p&gt; * * Three different objects of both PrinterSingleton and PrinterSubclass are * instantiated and compared. * * This Implementation treats the singleton property as a non-inherited * property. This meant that &lt;i&gt;Singleton&lt;/i&gt; classes can still be subclassed * and these subclasses can access the &lt;i&gt;Singleton&lt;/i&gt; constructor normally. * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrinterSingleton * @see PrinterSubclass */ class Main { /** * the three object references to instances of the &lt;i&gt;Singleton&lt;/i&gt; class. */ private static PrinterSingleton p1, p2, p3; /** * the three object references to instances of sublcasses of the * &lt;i&gt;Singleton&lt;/i&gt; class. */ private static PrinterSubclass ps1, ps2, ps3; /** * Implements the first test case. Creates 3 references to the * &lt;i&gt;Singleton&lt;/i&gt; by (a) using the regular constructor and then by (b) * using a factory method. (a) should fail, (b) should create three * identical objects. */ private static void test1() { System.out.println(&quot;Test 1a: Try to call regular constructor. Should &quot; + &quot;fail.&quot;); p1 = new PrinterSingleton(); p2 = new PrinterSingleton(); p3 = new PrinterSingleton(); p1.print(); p2.print(); p3.print(); System.out.println(&quot;\tOO Problem: Classes in the same package can &quot; + &quot;still access constructor.&quot;); System.out.println(&quot;Test 1b: Using instance() instead. Should create &quot; + &quot;three identical objects.&quot;); p1 = PrinterSingleton.instance(); p2 = PrinterSingleton.instance(); p3 = PrinterSingleton.instance(); p1.print(); p2.print(); p3.print(); } /** * Implements the first test case. Tests if the 3 objects from test 1 are * in fact identical */ private static void test2() { System.out.println(&quot;Test 2: All three objects should be identical&quot;); System.out.print(&quot;\tThey are &quot;); if ((p1 == p2) &amp;&amp; (p1 == p3)) { System.out.println(&quot;identical&quot;); } else {System.out.println(&quot;not identical&quot;); } } /** * Implements the first test case. Creates 3 instances of a &lt;i&gt;Singleton * &lt;/i&gt;'s subclass. These objects should be different. */ private static void test3() { System.out.println(&quot;Test 3: Ensuring that subclasses can access the&quot; + &quot;constructor&quot;); System.out.println(&quot; (All three outputs should be different)&quot;); ps1 = new PrinterSubclass(); ps2 = new PrinterSubclass(); ps3 = new PrinterSubclass(); ps1.print(); ps2.print(); ps3.print(); } /** * This is the driver for the &lt;code&gt;Singleton&lt;/code&gt; case. It performes * three tests: * * &lt;OL&gt; * &lt;LI&gt; Create 3 references to the &lt;i&gt;Singleton&lt;/i&gt; by (a) using the * regular constructor and then by (b) using a factory method. * (a) should fail, (b) should create three identical objects. * &lt;LI&gt; Test if the above 3 objects are in fact identical * &lt;LI&gt; Create 3 instances of a &lt;i&gt;Singleton&lt;/i&gt;'s subclass. These * objects should be different. * &lt;/OL&gt; */ public static void main (String[] args) { System.out.println(&quot;Testing pattern SINGLETON (java) ...&quot;); test1(); test2(); test3(); System.out.println(&quot;... done.&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.singleton.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 a sample class that will be assigned the &lt;i&gt;Singleton&lt;/i&gt; role * in this example. The class's functionality * is to store an instance-specific ID and provide a &lt;code&gt;print()&lt;/code&gt; * method that shows an object's ID. * * Note that in this implementation the class does not have to know * that it is a &lt;i&gt;Singleton&lt;/i&gt; (i.e. has to have appropriate code in it). * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrinterSubclass */ public class Printer { // Can just say to implement the interface here /** * Counts the instances of this class */ protected static int objectsSoFar = 0; /** * Each instance has an ID to distinguish them. */ protected int id; public Printer() { id = ++ objectsSoFar; } /** * Prints the instance's ID to &lt;code&gt;System.out&lt;/code&gt;. */ public void print() { System.out.println(&quot;\tMy ID is &quot;+id); } } </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 PrinterSingleton.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.singleton.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 sample &lt;i&gt;Singleton&lt;/i&gt; class. The class's functionality * is to store an instance-specific ID and provide a &lt;code&gt;print()&lt;/code&gt; * method that shows an object's ID. * * Note that in this implementation the &lt;i&gt;Singleton&lt;/i&gt; class has to know * that it implements the pattern (i.e. has to have appropriate code in it). * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrinterSubclass */ public class PrinterSingleton { /** * Counts the instances of this class */ protected static int objectsSoFar = 0; /** * Stores the &lt;i&gt;Singleton&lt;/i&gt;'s only instance */ protected static PrinterSingleton onlyInstance = null; /** * Each instance has an ID to distinguish them. */ protected int id; /** * Creates a new &lt;code&gt;PrinterSingleton&lt;/code&gt;. The new instance gets * an ID equal to the total number of instacnes of that type created +1. */ protected PrinterSingleton() { id = ++ objectsSoFar; } /** * Factory method that provides access to the &lt;i&gt;Singleton&lt;/i&gt; instance. * * returns the unique &lt;i&gt;Singleton&lt;/i&gt; instance */ public static PrinterSingleton instance() { if(onlyInstance == null) { onlyInstance = new PrinterSingleton(); } return onlyInstance; } /** * Prints the instance's ID to &lt;code&gt;System.out&lt;/code&gt;. */ public void print() { System.out.println(&quot;\tMy ID is &quot;+id); } } </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 PrinterSubclass.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.singleton.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 sample subclass of the &lt;i&gt;Singleton&lt;/i&gt; class. This class is * to test whether subclasses can still access the Singleton's constructor. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrinterSingleton */ public class PrinterSubclass extends PrinterSingleton { /** * Creates an instance of this class by calling &lt;code&gt;super()&lt;/code&gt;. */ public PrinterSubclass() { super(); } }</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 SingletonInstance.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.singleton.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): */ import ca.ubc.cs.spl.pattern.library.SingletonProtocol; /** * Implements a concrete instance of the &lt;i&gt;Singleton&lt;/i&gt; pattern. It declares * Printer to be Singleton and defines an exception to the Singleton * of the constructor: PrinterSubclass (a subclass of the Singleton) can still * access Printer's constructor. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Printer * @see PrinterSubclass */ public aspect SingletonInstance extends SingletonProtocol { /** * Assigns the Singleton to &lt;code&gt;Printer&lt;/code&gt; */ declare parents: Printer implements Singleton; // Alternatively, Printer can just declare to implement it /** * This declaration allows &lt;code&gt;PrinterSubclass&lt;/code&gt; (and all its * subclasses) to access &lt;code&gt;Printer&lt;/code&gt;'s constructor within * a its constructor (to allow for &lt;code&gt;super()&lt;/code&gt; calls). */ protected pointcut protectionExclusions(): call((PrinterSubclass+).new(..)); // To allow access to regular constructor (for subclasses etc.) }</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