1CMSC 433 – Programming Language Technologies and ParadigmsSpring 2007Singleton PatternMar. 13, 20072What is it?• If you need to make sure that there can be one and only one instance of a class.– For example, your system can have only one window manager or print spooler, or a single point of access to a database engine.3Approach 1• Embed a static variable inside the class that we set on the first instance and check for each time we enter the constructor. – A static variable is one for which there is only one instance, no matter how many instances there are of the class.• static boolean instance_flag = false;4Disadvantage of Approach 1• How to find out whether creating an instance was successful or not– Remember constructors do not return values. • One way would be to call a method that checks for the success of creation, and which simply returns some value derived from the static variable. – This is inelegant and prone to error• because there is nothing to keep you from creating many instances of such non-functional classes and forgetting to check for this error condition.5Approach 2• Create a class that throws an Exception when it is instantiated more than once.– Let’s create our own exception class for this case6Why a New Exception?• This new exception type doesn’t do anything in particular– other than calling its parent classes through the super() method,. • However, it is convenient to have our own named exception type so that the compiler will warn us of the type of exception we must catch when we attempt to create an instance7Lets Implement the Class8Lets Use it!9Disadvantage of Approach 2• Must enclose every method that may throw an exception in a try - catch block.• And the exception-based solution is not really “transparent”10Approach 3• There already is a kind of Singleton class in the standard Java class libraries: the Math class. – This is a class that is declared final and all methods are declared static, meaning that the class cannot be extended. – The purpose of the Math class is to wrap a number of common mathematical functions such as sin and log in a class-like structure, since the Java language does not support functions that are not methods in a class.• You can’t create any instance of classes like Math, and can only call the static methods directly in the existing final class.• You can use the same approach to a Singleton pattern, making it a final class.11Approach 312Disadvantage of Approach 3• Difficult to drop the restrictions of Singleton status– a lot of reprogramming to do to make the static approach allow multiple instances• this is easier to do in the exception style class structure13Approach 4• Create Singletons using a static method to issue and keep track of instances. • To prevent instantiating the class more than once, make the constructor private so an instance can only be created from within the static method of the class14Approach 415Approach 4• Don’t have to worry about exception handling if the singleton already exists-- you simply get a null return from the Instance method16Lets Use it!17Compile-time Error Checking• should you try to create instances of the iSpoolerclass directly, this will fail at compile time because the constructor has been declared as private.18Approach 5public class Singleton {// Private constructor suppresses generation// of a (public) default constructorprivate Singleton() {}private static class SingletonHolder {private static Singleton instance = new Singleton();}public static Singleton getInstance() {return SingletonHolder.instance;}}19Dropping the Singleton Requirement• Suddenly, we decide that we can allow “n”instances of the (previously) Singleton object• How would you adapt each of the previous five approaches?– Which implementation is
View Full Document