277294.ijbsn.asiaMain.java2004-03-24T16:00:00Z2004-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 "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 Singleton design pattern example.<p>
*
* Intent: <i>Ensure that a class has only one instance and provide a global
* point of access to it.</i><p>
*
* Participatng objects are <code>Printer</code> p1, p2, p3 and
* <code>PrinterSubclass</code> ps1, ps2, ps3.<p>
*
* Three different objects of both Printer and PrinterSubclass are
* instantiated and compared.
*
* This Implementation treats the singleton property as a non-inherited
* property. This meant that <i>Singleton</i> classes can still be subclassed
* and these subclasses can access the <i>Singleton</i> constructor normally.
*
* <p><i>This is the AspectJ version.</i><p>
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Printer
* @see PrinterSubclass
*/
public class Main {
/**
* the three object references to instances of the <i>Singleton</i> class.
*/
private static Printer p1, p2, p3;
// Experimental setup: Main creates three Printer objects.
// The Printer implementation gives each object a unique ID
// which is printed when print() is called. If the Singleton
// implementation works, all three objects should be the same.
//
// Implementation: AOP5 - One (concrete) aspect defines the behavior
// of the pattern, creating a generic getInstance() method that
// is attached to the Singleton interface. Another aspect assigns
// the role to a particular class.
//
// The general description of the pattern (lthough not in an
// abstract aspect) is reusable.
//
// Considers different signatures for the constructor.
//
// Clients don't have to type-cast, they just use new(..)
//
// Subclasses are automatically Singletons, too, unless
// explicitly declared as non-singletons
/**
* Implements the first test case. Creates 3 references to the
* <i>Singleton</i> by using the regular constructor. That should
* create three identical objects.
*/
private static void test1() {
System.out.println("Test 1: All three printers need to have the "
+ "same ID");
p1 = new Printer();
p2 = new Printer();
p3 = new Printer();
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("Test 2: All three printers need to be identical");
System.out.print("\tThey are ");
if ((p1 == p2) && (p1 == p3)) { System.out.println("identical"); }
else {System.out.println("not identical"); }
}
/**
* Implements the first test case. Creates 3 instances of a <i>Singleton
* </i>'s subclass. These objects should be different.
*/
private static void test3() {
System.out.println("Test 3: Ensuring that subclasses can access the"
+ "constructor");
System.out.println(" (All three outputs should be different)");
p1 = new PrinterSubclass();
p2 = new PrinterSubclass();
p3 = new PrinterSubclass();
p1.print();
p2.print();
p3.print();
}
/**
* This is the driver for the <code>Singleton</code> case. It performes
* three tests:
*
* <OL>
* <LI> Create 3 references to the <i>Singleton</i> by using the
* regular constructor. That should create three identical objects.
* <LI> Test if the above 3 objects are in fact identical
* <LI> Create 3 instances of a <i>Singleton</i>'s subclass. These
* objects should be different.
* </OL>
*/
public static void main (String[] args)
{
System.out.println("Testing pattern SINGLETON (aspectj) ...");
test1();
test2();
test3();
System.out.println("... done.");
}
}</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.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 "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 a sample class that will be assigned the <i>Singleton</i> role
* in this example. The class's functionality
* is to store an instance-specific ID and provide a <code>print()</code>
* method that shows an object's ID.
*
* Note that in this implementation the class does not have to know
* that it is a <i>Singleton</i> (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 <code>System.out</code>.
*/
public void print() {
System.out.println("\tMy ID is "+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:00ZPrinterSubclass.java2004-03-24T16:00:00Z2004-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 "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 a sample subclass of the <i>Singleton</i> 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 Printer {
/**
* Creates an instance of this class by calling <code>super()</code>.
*/
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:00ZSingletonInstance.java2004-03-24T16:00:00Z2004-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 "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):
*/
import ca.ubc.cs.spl.pattern.library.SingletonProtocol;
/**
* Implements a concrete instance of the <i>Singleton</i> 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 <code>Printer</code>
*/
declare parents: Printer implements Singleton; // Alternatively, Printer can just declare to implement it
/**
* This declaration allows <code>PrinterSubclass</code> (and all its
* subclasses) to access <code>Printer</code>'s constructor within
* a its constructor (to allow for <code>super()</code> 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: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