Singleton Pattern and the Class Loader
Singleton pattern is the most simplest and easiest of the design patterns that any designer/developer would be aware of. The purpose of a singleton pattern is to make sure that there is only one instance of an object throughtout the application’s life. Once an instance is created, the design makes it in a way that only that instance is used even if the program tries to create a new instance of it. The singleton object itself is lazily loaded meaning it is loaded only when the first use of the object is encountered. In essence Singleton objects have an application scope.
I was thinking that if I follow the design principles behind the singleton pattern I have a singleton object until I read through the JDC Tech Tips published in January this year. I thought this would be worth mentioning since I mentioned about class loaders few days back. According to the tip, if you are using a singleton pattern in your application and your application is a web application then which class loader in the application server loads the singleton class plays an important role in deciding really whether your class is a singleton or not. If the System class loader loads the singleton class and that instance is shared by all the other class loaders, then it is really a singleton object. If multiple class loaders were able to load the same class, then you dont really have a singleton object. Makes sense to me.










