In case you are new to the term singleton design pattern, It is recommended that, you read this article before proceeding further.
By this time I assume you know the basics and usefulness of a singleton design pattern.
Sometimes we endup in a situation where we can not use the singleton classes in a multiple threaded application. This could be for sevaral reasons like, making use of some data or some instance which are not thread-safe (eg. HashMap).
To create singleton instance per thread, we can make use of a ThreadLocal instance (java doc).
When we invoke get() on a ThreadLocal instance, it returns an independently initialized copy of the variable for each thread. ThreadLocal instances are typically private static fields in classes which we wish to make singleton per thread.
Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are garbage colelcted.
Example 1 - By using static reference of ThreadLocal
Now lets create the test method. Here we will print the instance hashcode with the thread name, so that we can conclude which instance is bound for a thread.
Run the test case -
This clearly shows, it is creating a unique instance per thread.
Example 2 - By using generic factory pattern
Step 1 : Create the singleton class
Step 2 : Create the interface
Step 3 : Create the generic ThreadLocalFactory utility class
Now create the generic ThreadLocalFactory which can convert any Factory to a ThreadLocal scope.
Step 4 : Create a singleton factory
Step 4 : Push the singleton factory to the thread local scope
We are done. Now we have created a Thread local singleton factory, which will return unique instance per thread.
Lets write the test method now.
Now lets run the test method -
This clearly demonstrates creation of unique objects per thread.
Dropdown (select element) is always read only. User do not edit it. User can select any of the option of its own chice. Unlike other html elements, it dont have a attribute which can make the element readony and prevent users to change its value.
So how do we do this, when we want to prevent the user not to edit its selected option.
There is a little trick. Why can’t we disable it.
Wait a second…
But as we are disabling this field, the field will not be the part of the request when the form is submitted.
We can do some work around for this.
So we need to use a hidden field to store disabled selected-option value. When the moment select element is disabled we need to create a hidden input field in the form and store the selected value. The moment it is enabled we need to restore the element with the value stored in the hidden field.
To count the number key/value pairs in a JSON object we need to convert an array. And then we can easily count the number of element in the array which is same as the number key value pairs in the json object.
Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found in the object.
When user clicks Select All checkbox, the code first checks the status of checkbox with id select-all, and loops though each checkbox with class checkbox update their checked property to true. When it is clicked again then it will update their checked property to false.