Using Singleton Pattern
Singleton pattern is gaining ground in the websphere commerce arena. It plays a major role in enhancing the application performance. Simply put, a singleton is a class that can be instantiated only once. This design pattern is very useful for handling data that is static(well almost). Any information that is frequently used and changes less frequently can be stored in a singleton. A singleton stores all the information in the memory and therefore is accessible without having to rebuild the information. Java world has an exhaustive description of what a singleton is all about and how you will go about building a singleton.
Examples of Usage
Here are a few examples of where you can use singleton design pattern in a websphere commerce applicaiton:
- Any static data such as the 50 states of the US. This data is accessed frequently and can be stored in a singleton. This prevents several trips to the database each time the states dropdown is loaded. Another such instance is the data coming from the properties files. This is the data that is used most frequently and can easily be stored in a singleton. Typically every commerce application will have its own properties file which is different from a bunch of property files that come packaged with every store that is published.
- All the category data, which rarely changes, can also be stored in a singleton. It is extremely useful if you can store the entire category tree in a singleton. Imagine the ability to get to a parent categoryId for a given categoryId or a set of all the child categoryIds for a given categoryId. This opens up a whole set of opportunities for building left navigation, bread crumbs and site maps. This is just the beginning.
- A very creative use of singletons is to store all the product data including name, description, partnumber etc in a singleton. Now the price information is likely to change frequently and new products can be added frequently. But with a good design it is possible to use this product data for implementing a commerce search functionality. I have implemented this search functionality on a site with fantastic results on the performance of site search functionality. This will be discussed in a future article.
The following shows an UML representation of the base classes for the singleton pattern. Notice that there is a resettable interface. An implementation of this interface implements the necessary reset functionality on the singleton - which typically calls the appropriate initialize method. Also, a SingletonManager object is used to manage all the singletons used in the application.
The following shows an UML representation of the CategoryTree Singleton. The meat of the code is initializeCategoryTree which loads all the category information in a hashmap of hashmaps. This hashmap is obtained using a recursion starting from the top level categories and going all the way to the last level categories.
Resetting Singletons
When the application starts, the singleton is usually built the first time the singleton is accessed. The good thing about this pattern is that you do not have to restart the application each time there is a change in the data. Here is where the resettable interface comes handy. When using singletons in a commerce application, it is important to provide a way for resetting the singleton. A simple JSP interface can show all the available singletons in the application using the SingletonManager. The user may be given an option to select an appropriate singleton to reset. Since each singleton implements the reset method, the JSP simply calls the reset method on the selected singleton. Additional capabilities for resetting singletons can be implemented through commerce scheduler commands. You can even call the reset methods on the singletons if you can trap the events that cause the data to change. The only place where things can get tricky is when the application is implemented in a clustered environment. The problem will be around the resetting of singletons and not the implementation itself. This will be a topic for an another day!